How can I add hours and minutes in a single query?
I tried this
Select DATEADD(hh,23.59,CAST('2016-07-08' AS DATETIME))
I need to ad 23 hours and 59 minutes, but my code only adds the hours. Why?
How can I add hours and minutes in a single query?
I tried this
Select DATEADD(hh,23.59,CAST('2016-07-08' AS DATETIME))
I need to ad 23 hours and 59 minutes, but my code only adds the hours. Why?
6条答案
按热度按时间gzszwxb41#
How about this?
EDIT:
If you are getting a float/decimal value such as 23.59, then you can do:
Note: You can also use:
But I find this usage of the modulo operator slightly off-kilter.
mrwjdhj32#
Use the below code for adding time part to a date.
drkbr07n3#
You can also use
DATEADD()
twice:epfja78i4#
The
datepart
argument, in your casehh
, is part of date to which an integer number is added. So theDATEADD
function just ignores any fractional part of your input.To get result you want, you need to either use
DATEADD
twice, adding 23 hours and then 59 minutes, or add one day to your date and subtract 1 minute, like this:See also Microsoft's documentation on
DATEADD
.7cwmlq895#
another method : you can do by adding one day and then substract minute
Output is same
yzuktlbb6#
In Oracle SQL I do it like this and it work fine to me