Suppose I have a datetime field whose value is 2000-01-01 08:30:00 and a duration field whose value is say 00:15 (meaning 15 minutes)
If I subtract these two, I should get 2000-01-01 08:15:00
Also if I want to subtract 1:15 (means 1 hour 15 minutes), the output should be 2000-01-01 07:15:00
I am trying SELECT DATEDIFF(minute, '00:15','2000-01-01 08:30:00');
But the output is 52595055. How can i get the desired result?
N.B.~ If I do SELECT dateadd(minute, -15,'2000-01-01 08:30:00');
, I will get the desired result but that involves parsing the minute field.
Edit:
As per the answers, every one is suggesting converting everything into minutes and then to subtract - so if it is 1:30, i need to subtract 90 minutes. That's fine. Any other way without converting to minutes?
I spent a while trying to do the same thing, trying to subtract the
hours:minutes
fromdatetime
- here's how I did it:OUTPUT:
You want to use DATEADD, using a negative duration. e.g.
The second value (-15 in this case) must be numeric (i.e. not a string like '00:15'). If you need to subtract hours and minutes I would recommend splitting the string on the : to get the hours and minutes and subtracting using something like
where @h is the hour part of your string and @m is the minute part of your string
EDIT:
Here is a better way:
Have you tried
DATEDIFF is the difference between 2 dates.
Use DATEPART to pull apart your interval, and DATEADD to subtract the parts:
or, we can convert to minutes first (though OP would prefer not to):