SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Problem with dateDiff
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

meltingchain
Starting Member

3 Posts

Posted - 06/21/2013 :  15:41:14  Show Profile  Reply with Quote
I'm trying to find the difference in hours between SomeTime and Midnight by using dateDiff

datediff(mi,Convert(Time,SomeTime), Convert(time,'23:59:59'))

However because Converting it into time i cannot use 24:00:00 and if i use 00:00:00 i get a negative number because it calculates backwards.
is there a way to get the correct amount, so that when i convert it i wont be losing data.

James K
Flowing Fount of Yak Knowledge

3742 Posts

Posted - 06/21/2013 :  16:01:52  Show Profile  Reply with Quote
quote:
Originally posted by meltingchain

I'm trying to find the difference in hours between SomeTime and Midnight by using dateDiff

datediff(mi,Convert(Time,SomeTime), Convert(time,'23:59:59'))

However because Converting it into time i cannot use 24:00:00 and if i use 00:00:00 i get a negative number because it calculates backwards.
is there a way to get the correct amount, so that when i convert it i wont be losing data.

I didn't quite follow what the problem you are trying to solve is, but see if this does what you are trying to do. Also, dividing by 60 to convert to hours.
DATEDIFF(mi,Convert(Time,SomeTime),'19000102')/60.0
Go to Top of Page

meltingchain
Starting Member

3 Posts

Posted - 06/21/2013 :  16:22:51  Show Profile  Reply with Quote
When i was trying to find the date difference from someTime (lets say 17:00:00) to midnight i had to use 23:59 but i lost a minute.

yes that did fix it thank you. if you could also explain to me why it works ill greatly appreciate it.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30281 Posts

Posted - 06/21/2013 :  17:14:16  Show Profile  Visit SwePeso's Homepage  Reply with Quote
1 + datediff(mi,Convert(Time,SomeTime), Convert(time,'23:59:59'))


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/22/2013 :  02:45:56  Show Profile  Reply with Quote
DATEDIFF(mi,YourTimeVariable,1)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000