Solved

Subtract two dateTime fields

Posted on 2009-03-31
10
1,055 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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

792 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