Solved

total of days in sql

Posted on 2013-01-29
5
278 Views
Last Modified: 2013-02-14
I want to get the total days in sql from two different dates.

2011-06-02 15:00:00.000
2011-11-02 15:00:00.000
0
Comment
Question by:VBdotnet2005
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38832044
SELECT DATEDIFF(dd, '2011-06-02', '2011-11-02' )
0
 
LVL 11

Expert Comment

by:Simone B
ID: 38832052
SELECT DATEDIFF(d,'2011-06-02 15:00:00.000','2011-11-02 15:00:00.000')
0
 
LVL 11

Assisted Solution

by:Simone B
Simone B earned 250 total points
ID: 38832072
When you use d or dd, the time is ignored. If you want to round to the closest number of days, you can include the time, calculated the difference in hours, then divide by 24:

Here's an example using different time values. It will give you 152 instead of 153:

SELECT DATEDIFF(hh,'2011-06-02 23:00:00.000','2011-11-02 00:00:00.000')/24
0
 
LVL 9

Expert Comment

by:mimran18
ID: 38832217
Select datediff(day,'2011-06-02 15:00:00.000','2011-11-02 15:00:00.000') +1
0
 
LVL 10

Accepted Solution

by:
Monica P earned 250 total points
ID: 38846505
DAy difference

SELECT DATEDIFF(day,'2008-08-05','2008-06-05') AS DiffDate



minutes
-----------------
SELECT DATEDIFF(MI,'2011-10-14 02:18:58' , '2011-10-14 02:19:01') AS MIN_DIFF


if need accuracy in seconds too then

seconds
----------------
SELECT DATEDIFF(second,'2011-10-14 02:18:58' , '2011-10-14 02:19:01')/60.0 AS MIN_DIFF




get all from one using SP

select
        *,
        Days          = datediff(dd,0,DateDif),
        Hours         = datepart(hour,DateDif),
        Minutes       = datepart(minute,DateDif),
        Seconds       = datepart(second,DateDif),
        MS            = datepart(ms,DateDif)
from
        (
        select
                DateDif = EndDate-StartDate,
                aa.*
        from
                (  -- Test Data
                Select
                        StartDate = convert(datetime,'20130715 02:44:37.923'),
                        EndDate   = convert(datetime,'20130730 13:24:45.837')
                ) aa
        ) a
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now