2

I have 4 columns that are

Startdate, 
enddate, 
starttime, 
endtime.  

I need to get subtractions from the enddate and endtime - startdate and starttime. I will need that answer from the four columns in sql.

so far I have this I found but dont think this will work right.

SELECT DATEDIFF (day, enddate, startdate) as NumberOfDays  
DATEDIFF(hour,endtime,starttime) AS NumberOfHours 
DATEDIFF(minute,endtime,starttime) AS NumberOfMinutes 
from table;

Thanks for your help

StuartLC
  • 104,537
  • 17
  • 209
  • 285
user3537618
  • 21
  • 1
  • 1
  • 3
  • Would you mind positing examples of input data with your expected return dataset please? – MarkD May 07 '14 at 14:49
  • sample date would be 4/1/2014 sample time would be 4:38. I would like the data to return something like 1 day, 10 hours, 15 minutes or 10 hours and 30 mintues. – user3537618 May 07 '14 at 14:51
  • 1
    You wrote: "I have 4 columns that are Startdate, enddate, starttime, endtime." Please post samples of this data as well as expected output. – MarkD May 07 '14 at 14:53
  • MarkD, i have put the sample data in – user3537618 May 07 '14 at 15:02
  • Given that Sql 2000 doesn't have `DATE` and `TIME` types, can we assume that they are (or are castable to) `DATETIMEs', in which you should be able to just add them as per `http://stackoverflow.com/q/700619/314291` (assuming that the time bit is stored as '1900-01-01 HH:MM:SS'. This begs the question - why wasn't this modelled as a datetime (and I guess time to upgrade Sql)? – StuartLC May 07 '14 at 15:07

2 Answers2

1

EDIT - Now that I realize that the question is for SQL Server 2000, this proposed answer may not work.

The SQL Server 2000 documentation can be found at https://www.microsoft.com/en-us/download/details.aspx?id=18819. Once installed, look for tsqlref.chm in your installed path, and in that help file you can find information specific to DATEDIFF.


Based on the wording of the original question, I'm assuming that the start/end time columns are of type TIME, meaning there is no date portion. With that in mind, the following would answer your question.

However, note that depending on your data, you will lose precision in regards to the seconds and milliseconds.

More about DATEDIFF: https://msdn.microsoft.com/en-us/library/ms189794.aspx


DECLARE @mytable AS TABLE 
    (
        startdate DATETIME,
        enddate DATETIME,
        starttime TIME,
        endtime TIME
    )


INSERT INTO @mytable (startdate, enddate, starttime, endtime)
VALUES      (GETDATE() - 376, GETDATE(), '00:00:00', '23:59')

SELECT      *
FROM        @mytable

SELECT      DATEDIFF(HOUR, startdate, enddate) AS [NumHours],
            DATEDIFF(MINUTE, starttime, endtime) AS [NumMinutes]
FROM        @mytable

This would yield output similar to:

enter image description here

dev8675309
  • 356
  • 4
  • 13
0

Assuming you have data like this, you can add the StartDate to the StartTime to get the StartDateTime, same for the EndDateTime

StartDate                StartTime                EndDate                  EndTime              
-----------------------  -----------------------  -----------------------  -----------------------
2014-05-01 00:00:00.000  1900-01-01 10:53:28.290  2014-05-07 00:00:00.000  1900-01-01 11:55:28.290

Once you've done that you can get the Days, Hours and Minutes like this:

select 
DATEDIFF(minute, StartDate + StartTime, EndDate + EndTime) / (24*60) 'Days',
(DATEDIFF(minute, StartDate + StartTime, EndDate + EndTime) / 60) % 24 'Hours',
DATEDIFF(minute, StartDate + StartTime, EndDate + EndTime) % 60 'Minutess'
  from YourTable

We have work in minutes the whole time in order to prevent problems with partial days crossing midnight and partial hours crossing an hour mark.

Jeremy Hutchinson
  • 1,975
  • 16
  • 26
  • 1
    If the times are `12:31` and `14:29` this will return `2 hours` and `58 minutes` *(it should only be `1 hour` and `58 minutes`)*. Instead, all of the `DATEDIFF`s should be in minutes. `Days` having `/ (24 * 60)` and `hours` having `/ 60) % 24` and `minutes` having `% 60`. – MatBailie May 07 '14 at 15:31
  • Thank you @MatBailie, I had accounted for that problem in the days calculation, but forgot the hours. I think the Days calculation was OK with datediff by hour / 24, but for safety sake I left the answer as /(24*60) – Jeremy Hutchinson May 07 '14 at 15:55