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

Can I convert a string to a time field ? Expand / Collapse
Author
Message
Posted Thursday, May 1, 2014 3:39 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 2:00 PM
Points: 295, Visits: 488
Hi

I have a field that is a string, but stores a time value as "09:45 AM"

I need to do calculations on time differences
for example

Starttime has the value "09:45 AM"
Endtime has the value "10:15 AM"

I need to show Endtime - starttime = "30 Minutes"


Thanks
Joe
Post #1566862
Posted Friday, May 2, 2014 2:33 AM This worked for the OP Answer marked as solution


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:56 AM
Points: 7,120, Visits: 13,491
SELECT 
Starttime = CAST(Starttime AS TIME),
Endtime = CAST(Endtime AS TIME),
MinutesElapsed = DATEDIFF(minute, CAST(Starttime AS TIME), CAST(Endtime AS TIME))
FROM (SELECT Starttime = '09:45 AM', Endtime = '10:15 AM') d



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1566955
Posted Friday, May 2, 2014 7:46 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 2:00 PM
Points: 295, Visits: 488
Thanks Chris

Worked great, I guess I'll need to use CAST alot with the data in this table :)

Have a nice weekend!!
Post #1567032
Posted Friday, May 2, 2014 8:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:56 AM
Points: 7,120, Visits: 13,491
You're welcome, you too! It's a bank holiday weekend here

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1567043
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse