Solved

Subtract two dateTime fields

Posted on 2009-03-31
10
1,054 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i use WITH CTE for checking exist value? 3 32
SQL Improvement  ( Speed) 14 26
Update a text value in another table 10 39
Can > be used for a Text field 6 39
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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

776 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