Solved

Subtract two dateTime fields

Posted on 2009-03-31
10
1,058 Views
Last Modified: 2012-05-06
How do I two subtract a DateTime fields in sql
Feild1 = 2009/04/05 08:00, Field2 = 2009/05/02 09:00
The dates should not be counted in the subtraction. The value to be returned should be 01:00.
0
Comment
Question by:u2envy1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 16

Assisted Solution

by:brad2575
brad2575 earned 50 total points
ID: 24028461
DATEDIFF ( datepart , startdate , enddate )

date parts listed here:
http://msdn.microsoft.com/en-us/library/ms189794.aspx
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 450 total points
ID: 24028472
Try something like this:


SELECT Field1, Field2, CONVERT(datetime, CONVERT(varchar, Field2, 108)) - CONVERT(datetime, CONVERT(varchar, Field1, 108)) AS Diff
FROM SomeTable
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24028486
The "round trip" CONVERT expressions have the effect of stripping the date portions of the datetime values;
similarly, had I used the format code 101 instead of 108, it would have stripped the time portions.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:u2envy1
ID: 24028589
matthewspatrick:
The CONVERT is not stripping the date properly. Im getting 12/31/1899 & 01/01/1900
0
 

Author Comment

by:u2envy1
ID: 24028634
If I have a time Value of 08:00 AM & 07:00 PM I would like a value of 12:00 returned with no AM / PM.
Will the Convert approach do this for me ?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24028635
u2envy1,

datetime values always encode a date and a time.  To enforce formatting:

SELECT Field1, Field2, CONVERT(varchar, CONVERT(datetime, CONVERT(varchar, Field2, 108)) - CONVERT(datetime, CONVERT(varchar, Field1, 108)), 108) AS Diff
FROM SomeTable

Regards,

Patrick
0
 

Author Comment

by:u2envy1
ID: 24028713
Thx Matthew.....
How do I convert the As Diff to show hh:mm only. No seconds ?
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 450 total points
ID: 24028741
SELECT Field1, Field2, LEFT(CONVERT(varchar, CONVERT(datetime, CONVERT(varchar, Field2, 108)) - CONVERT(datetime, CONVERT(varchar, Field1, 108)), 108), 5) AS Diff
FROM SomeTable
0
 

Author Closing Comment

by:u2envy1
ID: 31564795
Thx, its working perfectly......
0
 

Author Comment

by:u2envy1
ID: 24036338
One more thing. Is it possible to Sum(Diff) ?
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question