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


How to get difference between two dates in days and hours?


How to get difference between two dates in days and hours?

Author
Message
Srikanth 21
Srikanth 21
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 275
Hi, I'm looking code to find difference between two dates in days and hours.
Example:
date1: 2013-04-07 14:45:41.013 - date2: 2013-04-05 10:45:41.013

I need output like 2 days and 6 hours?
bitbucket-25253
bitbucket-25253
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56379 Visits: 25280
Use the DATEDIFF functions ...

for example

SELECT DATEDIFF ( datepart , startdate , enddate )

This will return the number of days difference.

SELECT DATEDIFF(d,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')
returns 2 days

This returns the total number of hours difference (52)

SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')

--combining every thing I think will give you what you are looking for:

   SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')/24 AS 'Days'

,DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013') -

(DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')/24)*24 AS 'Hours'
Result:

Days Hours
2 4



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Srikanth 21
Srikanth 21
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 275
Thanks, It's worked.
dsharmadbec
dsharmadbec
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 2
bitbucket-25253 - Sunday, April 7, 2013 2:05 PM
Use the DATEDIFF functions ... for example SELECT DATEDIFF ( datepart , startdate , enddate )This will return the number of days difference.SELECT DATEDIFF(d,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')returns 2 daysThis returns the total number of hours difference (52)SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')--combining every thing I think will give you what you are looking for:
   SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')/24 AS 'Days'        ,DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013') -         (DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')/24)*24 AS 'Hours'ResultBigGrinays   Hours2   4 


This will only works, when minutes and seconds are same of both the dates, but if we change minutes then values are not correct.
Like: SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:00:41.013')
I have changed the minutes in this query,
After executing this query we still get the same output i.e. 4 hours
But According to me output should be 3 hours,
Can you tell me how we can get 3 hours output.
Please help

Thanks
Deepak Sharma

george_at_sql
george_at_sql
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1187 Visits: 1723
Since the date you are comparing has a precision at the microsecond level you would need to use the precision you are using to determine the difference between dates..

SELECT DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015') /*Difference in Micro seconds between two dates*/
,DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015')/1000/60/60 as hrs_diff /*Convert the difference in Micro Seconds as Hours*/
,(DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015')/1000/60) % 60 as min_diff /*Gets the residual minutes*/

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)

Group: General Forum Members
Points: 380712 Visits: 42216
dsharmadbec - Tuesday, March 13, 2018 8:00 AM
bitbucket-25253 - Sunday, April 7, 2013 2:05 PM
Use the DATEDIFF functions ... for example SELECT DATEDIFF ( datepart , startdate , enddate )This will return the number of days difference.SELECT DATEDIFF(d,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')returns 2 daysThis returns the total number of hours difference (52)SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')--combining every thing I think will give you what you are looking for:
   SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')/24 AS 'Days'        ,DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013') -         (DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:45:41.013')/24)*24 AS 'Hours'ResultBigGrinays   Hours2   4 


This will only works, when minutes and seconds are same of both the dates, but if we change minutes then values are not correct.
Like: SELECT DATEDIFF(hh,'2013-04-05 10:45:41.013','2013-04-07 14:00:41.013')
I have changed the minutes in this query,
After executing this query we still get the same output i.e. 4 hours
But According to me output should be 3 hours,
Can you tell me how we can get 3 hours output.
Please help

Thanks
Deepak Sharma


You need to understand that DATEDIFF is counting the number of boundaries you are crossing based on the date type you are using, which in your case is hours (hh).

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sgmunson
sgmunson
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86230 Visits: 6734
george-178499 - Tuesday, March 13, 2018 8:24 AM
Since the date you are comparing has a precision at the microsecond level you would need to use the precision you are using to determine the difference between dates..

SELECT DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015') /*Difference in Micro seconds between two dates*/
,DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015')/1000/60/60 as hrs_diff /*Convert the difference in Micro Seconds as Hours*/
,(DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015')/1000/60) % 60 as min_diff /*Gets the residual minutes*/

Also, your DATEDIFF is choosing milliseconds, not microseconds.


Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)SSC Guru (847K reputation)

Group: General Forum Members
Points: 847799 Visits: 46682
First, for all those that are about to remind us that this post was necro'd from a 5 year old post, we already know that. Please drive through. Wink

What the original post is looking for is a simple "interval" or "duration". Please see the following article for a simple way to do this and modify the formatting section for however you see fit.

Calculating Duration Using DATETIME Start and End Dates (SQL Spackle)

--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
Sergiy
Sergiy
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96418 Visits: 13941
Or use a function described in this article:

http://www.sqlservercentral.com/articles/Datetime+conversions/153316/
george_at_sql
george_at_sql
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1187 Visits: 1723
sgmunson - Tuesday, March 13, 2018 12:57 PM
george-178499 - Tuesday, March 13, 2018 8:24 AM
Since the date you are comparing has a precision at the microsecond level you would need to use the precision you are using to determine the difference between dates..

SELECT DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015') /*Difference in Micro seconds between two dates*/
,DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015')/1000/60/60 as hrs_diff /*Convert the difference in Micro Seconds as Hours*/
,(DATEDIFF(ms,'2013-04-07 10:45:41.013','2013-04-07 14:00:41.015')/1000/60) % 60 as min_diff /*Gets the residual minutes*/

Also, your DATEDIFF is choosing milliseconds, not microseconds.

Oh yea :-). The precision is at the millisecond level. The output computed it at the milli second itself though.

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