SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Perform math on string


Perform math on string

Author
Message
jbuttery
jbuttery
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 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.
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10330 Visits: 10575
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
Author - SQL Server T-SQL Recipes
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

GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24863 Visits: 9730
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
andrewd.smith
andrewd.smith
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1584 Visits: 3232
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.
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10330 Visits: 10575
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
Author - SQL Server T-SQL Recipes
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

jbuttery
jbuttery
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 133
Thanks guys. I've been away from SQL for a while. This really helped.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91390 Visits: 41151
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! :-P Wonder if the developer likes point blank pork chops? Hehe

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10330 Visits: 10575
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
Author - SQL Server T-SQL Recipes
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

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91390 Visits: 41151
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24863 Visits: 9730
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search