Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Subtracting Times?

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2007-03-01 : 11:55:49
I'd like to take two DateTimes, subtract them and insert the result as HH:MM:SS,

I think you use datediff but I'm not clear on the datediff formatting to get back HH:MM:SS...

Thanks for your help!


--PhB

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-01 : 12:00:48
DECLARE @s datetime, @e datetime
SELECT @s = '2001-12-31 11:59:25', @e = '2001-12-31 10:45:13'
SELECT CONVERT(varchar(10),(@s-@e),108)





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

phrankbooth
Posting Yak Master

162 Posts

Posted - 2007-03-01 : 13:26:54
Hhmmmmm, Two things with this.

1: If you change the date @s to '2001-12-30 11:59:25', the query still results in the same time amount. It should be greater, no?

2: The time value result from above has to be a varchar?

Thank you!

--PhB
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-01 : 13:39:57
quote:
Originally posted by phrankbooth

Hhmmmmm, Two things with this.

1: If you change the date @s to '2001-12-30 11:59:25', the query still results in the same time amount. It should be greater, no?



No, you are only displaying the time component with the CONVERT Function. Are you looking to add 24 hours, so that you include the Date component? If so, you'll need to do more work

quote:

2: The time value result from above has to be a varchar?



Yes

quote:

Thank you!



You're welcome


--PhB
[/quote]

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-01 : 14:48:16
phrankbooth --

this page may help you out a little:
http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx


Lots of datetime functions to use and some that help you work with timespans.

Remember that if you substract two times, the "offset" number of days is from 1/1/1900.

so, if I substract 5:00PM from 10:00PM, the result is:

1/1/1900 5:00AM

which is five hours into day 0.

if you substract 6:00PM from 5:00PM, the result is:

12/31/1899 11:00PM

which is basically -1 hours from 1/1/1900 12:00:00 AM.

Write some simple SQL statements and play around with this until you get the idea.


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

phrankbooth
Posting Yak Master

162 Posts

Posted - 2007-03-01 : 21:10:55
Ooooh, juicy page! Uhthangyuhverahmush.

--PhB
Go to Top of Page
   

- Advertisement -