Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

varchar to time or datetime or ARGGHH Please help me. Expand / Collapse
Author
Message
Posted Wednesday, September 26, 2012 8:56 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 11:25 AM
Points: 1,945, Visits: 3,183
I have a datetime (your standard yyyy-mm-dd hh:mm:ss) column and a varchar column that holds an appt time that looks like this: 1030 or 0735. I need to be able to calculate a difference to determine a wait time.


Wish we had DDL, but the first nameless column is DATETIME2(0) and the second one TIME(0) or another DATETIME2(0) with the complete timestamp in it. If you use TIME(0), then you can use "CAST (nameless AS DATE) + appointment_time" to get the second argument for a DATEDIFF() call.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1365008
Posted Wednesday, September 26, 2012 9:04 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:42 AM
Points: 3,440, Visits: 5,398
CELKO (9/26/2012)
I have a datetime (your standard yyyy-mm-dd hh:mm:ss) column and a varchar column that holds an appt time that looks like this: 1030 or 0735. I need to be able to calculate a difference to determine a wait time.


Wish we had DDL, but the first nameless column is DATETIME2(0) and the second one TIME(0) or another DATETIME2(0) with the complete timestamp in it. If you use TIME(0), then you can use "CAST (nameless AS DATE) + appointment_time" to get the second argument for a DATEDIFF() call.


Interesting approach. CASTing to TIME just seemed a little simpler as presumably they won't keep their patients waiting for more than a day.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1365009
Posted Thursday, September 27, 2012 7:15 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:38 PM
Points: 13,328, Visits: 12,825
dwain.c (9/26/2012)
Nice set up data Sean!

Using it, I'd like to offer a slightly less verbose solution:

SELECT *, WaitTime=DATEDIFF(minute
,CAST(check_in AS TIME)
,CAST(STUFF(appt_time, 3, 0, ':') AS TIME))
FROM #CheckIn


The STUFF apparently forces the datatype to VARCHAR so minutes aren't lost.

Both solutions return negative minutes, so you may want to take that into account.


That's pretty cool Dwain. Didn't think of using stuff, I went the hard away around.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1365230
Posted Monday, October 1, 2012 1:08 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 5:45 AM
Points: 1,294, Visits: 84
Hi,

This is vivek, how to change string to int.may be you can change date/int to char.

Thanks,
vivek
Post #1366721
Posted Tuesday, October 2, 2012 11:20 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, December 5, 2014 12:25 PM
Points: 12, Visits: 21
BEGIN
DECLARE @return_dt DATETIME;

SET @return_dt = CAST(CONVERT(varchar(11), CAST(@appt_date AS DATETIME), 120)
+ ' ' + SUBSTRING(@appt_time, 1, 2)
+ ':' + SUBSTRING(@appt_time, 3, 2)
+ ':00' AS DATETIME)

RETURN @return_dt
END
GO


Returns datetime. Thanks all for your help.
Post #1367193
Posted Tuesday, October 2, 2012 4:26 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:30 PM
Points: 2,332, Visits: 3,512
dwain.c (9/26/2012)
Nice set up data Sean!

Using it, I'd like to offer a slightly less verbose solution:

SELECT *, WaitTime=DATEDIFF(minute
,CAST(check_in AS TIME)
,CAST(STUFF(appt_time, 3, 0, ':') AS TIME))
FROM #CheckIn


The STUFF apparently forces the datatype to VARCHAR so minutes aren't lost.

Both solutions return negative minutes, so you may want to take that into account.




I think it needs one (semi)slight adjustment:

SELECT *, WaitTime=DATEDIFF(minute
,CAST(check_in AS TIME)
,CAST(STUFF(RIGHT('000' + CAST(appt_time AS varchar(4)), 4), 3, 0, ':') AS TIME))
FROM #CheckIn


SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1367326
Posted Tuesday, October 2, 2012 6:35 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:42 AM
Points: 3,440, Visits: 5,398
ScottPletcher (10/2/2012)
dwain.c (9/26/2012)
Nice set up data Sean!

Using it, I'd like to offer a slightly less verbose solution:

SELECT *, WaitTime=DATEDIFF(minute
,CAST(check_in AS TIME)
,CAST(STUFF(appt_time, 3, 0, ':') AS TIME))
FROM #CheckIn


The STUFF apparently forces the datatype to VARCHAR so minutes aren't lost.

Both solutions return negative minutes, so you may want to take that into account.




I think it needs one (semi)slight adjustment:

SELECT *, WaitTime=DATEDIFF(minute
,CAST(check_in AS TIME)
,CAST(STUFF(RIGHT('000' + CAST(appt_time AS varchar(4)), 4), 3, 0, ':') AS TIME))
FROM #CheckIn


Scott - Point taken. Yours works a lot better when there are no leading zeroes on the time component.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1367358
Posted Wednesday, October 3, 2012 4:01 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, December 5, 2014 12:25 PM
Points: 12, Visits: 21
Sean Lange (9/27/2012)
dwain.c (9/26/2012)
Nice set up data Sean!

Using it, I'd like to offer a slightly less verbose solution:

SELECT *, WaitTime=DATEDIFF(minute
,CAST(check_in AS TIME)
,CAST(STUFF(appt_time, 3, 0, ':') AS TIME))
FROM #CheckIn


The STUFF apparently forces the datatype to VARCHAR so minutes aren't lost.

Both solutions return negative minutes, so you may want to take that into account.


That's pretty cool Dwain. Didn't think of using stuff, I went the hard away around.
Post #1368076
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse