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 123»»»

Perform math on string Expand / Collapse
Author
Message
Posted Friday, April 24, 2009 11:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:40 AM
Points: 46, Visits: 133
My developer is storing the time as a 6 char string, e.g. 133737 for 1:37:37 PM. I'm trying to subtract fields to determine number of hours minutes seconds between events for elapsed time, but I can't seem to get CAST or CONVERT to give me the required format.

Row 1 154040
Row 2 133737

So, (Row 1 - Row 2) = 2h3m3s or 2.05h

The sun is bright today, but I'm not.
Post #704185
Posted Friday, April 24, 2009 12:30 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:06 PM
Points: 6,543, Visits: 8,751
First, fire your developer and store the time in a datetime field.

In the meantime, use this code:
declare @Start char(6), @End char(6)

set @Start = '133737'
set @End = '154040'

declare @StartTime datetime, @EndTime datetime
set @StartTime = stuff(stuff(@Start,3,0,':'),6,0,':')
set @EndTime = stuff(stuff(@End,3,0,':'),6,0,':')

select @StartTime, @EndTime, convert(char(8), @EndTime - @StartTime, 108)

After you've changed the fields to datetime, all you would have to do is:
select convert(char(8), EndDate - StartDate, 108)



Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #704234
Posted Friday, April 24, 2009 12:40 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 1:34 PM
Points: 15,442, Visits: 9,588
Before you start stuffing the string, make sure that times before 10 AM are stored with leading zeroes. If not, you might have a problem when 9:00 AM tries to turn into the hour 90.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #704242
Posted Friday, April 24, 2009 12:43 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 24, 2012 2:12 PM
Points: 1,213, Visits: 3,232
By converting the time strings to datetime values, you can do the following.

/* I'm assuming here that @TimeString1 always represents the earlier time and @TimeString2 the later time */
DECLARE @TimeString1 char(6)
DECLARE @TimeString2 char(6)
SELECT @TimeString1 = '133737', @TimeString2 = '154040'

/* Insert ':' characters to format as 'HH:MM:SS' and implicitly convert strings to datetime values */
DECLARE @Time1 datetime
DECLARE @Time2 datetime
SELECT @Time1 = STUFF(STUFF(@TimeString1, 5, 0, ':'), 3, 0, ':'),
@Time2 = STUFF(STUFF(@TimeString2, 5, 0, ':'), 3, 0, ':')

/* Determine time difference as a datetime value relative to value 0 (1900-01-01 00:00:00.000). Note that if @Time1 > @Time2 then the result of the DATEDIFF function call is negative. In this case, it is assumed that the two times span a day boundary so 24 * 60 * 60 = 86400 seconds need to be added to get the true elapsed time. */
DECLARE @ElapsedTime datetime
SELECT @ElapsedTime = DATEADD(second, DATEDIFF(second, @Time1, @Time2) + CASE WHEN (@Time1 > @Time2) THEN 86400 ELSE 0 END, 0)

/* Finally display elapsed time using CONVERT format 108 */
SELECT CONVERT(char(8), @ElapsedTime, 108)

If elapsed times equal or exceed 24 hours then the above method won't work and you will need to consider dates as well.

It would have been simpler and more efficient to store the times as datetime values in the table, and if you are storing the date in a separate column, then it would probably be better to store the date and time combined in a single datetime column as well.
Post #704245
Posted Friday, April 24, 2009 12:51 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:06 PM
Points: 6,543, Visits: 8,751
Here's a one-command approach for the first one I did. It's uglier, but you don't have to call any procedures / functions.

select convert(char(8), 
convert(datetime,
convert(datetime, stuff(stuff('154040',3,0,':'),6,0,':')) -
convert(datetime, stuff(stuff('133737',3,0,':'),6,0,':'))),
108)



Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #704250
Posted Friday, April 24, 2009 1:29 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:40 AM
Points: 46, Visits: 133
Thanks guys. I've been away from SQL for a while. This really helped.
Post #704274
Posted Friday, April 24, 2009 3:51 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:41 PM
Points: 35,944, Visits: 30,229
WayneS (4/24/2009)
First, fire your developer and store the time in a datetime field.


BWAA-HAA-HAA!!!! Dammit! Ya beat me to it! Wonder if the developer likes point blank pork chops?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #704339
Posted Friday, April 24, 2009 7:55 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:06 PM
Points: 6,543, Visits: 8,751
GSquared (4/24/2009)
Before you start stuffing the string, make sure that times before 10 AM are stored with leading zeroes. If not, you might have a problem when 9:00 AM tries to turn into the hour 90.

Hmm, good point.

How about this?
select convert(char(8),
convert(datetime,
convert(datetime, stuff(stuff(right('0' + '154040',6),3,0,':'),6,0,':')) -
convert(datetime, stuff(stuff(right('0' + '133737',6),3,0,':'),6,0,':'))),
108)



Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #704369
Posted Saturday, April 25, 2009 9:08 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:41 PM
Points: 35,944, Visits: 30,229
WayneS (4/24/2009)
GSquared (4/24/2009)
Before you start stuffing the string, make sure that times before 10 AM are stored with leading zeroes. If not, you might have a problem when 9:00 AM tries to turn into the hour 90.

Hmm, good point.

How about this?
select convert(char(8),
convert(datetime,
convert(datetime, stuff(stuff(right('0' + '154040',6),3,0,':'),6,0,':')) -
convert(datetime, stuff(stuff(right('0' + '133737',6),3,0,':'),6,0,':'))),
108)



Good idea... but what about times before 1 AM? For example, 000001 should be interpreted as 00:00:01 and it probably won't because it seems that all the leading zeros are dropped... probably because the original time was stored as an INT or something odd. I say "odd", but even Microsoft made the same terrible mistake in the MSDB.dbo.SysJobsHistory.

With that in mind, you might want to change the code to look like this...
select convert(char(8),
convert(datetime,
convert(datetime, stuff(stuff(right('000000' + '154040',6),3,0,':'),6,0,':')) -
convert(datetime, stuff(stuff(right('000000' + '133737',6),3,0,':'),6,0,':'))),
108)

Still, that doesn't solve the real problem... the real problem is that the times aren't stored as a DATETIME datatype which would greatly simplify such mathematics.



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #704465
Posted Monday, April 27, 2009 7:28 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 1:34 PM
Points: 15,442, Visits: 9,588
To deal with times that don't have enough leading zeroes, you can either padd the string, or you can reverse it. Either one works.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #704911
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse