How to add time to DateTime in SQL
I'm trying to add custom time to datetime in SQL Server 2008 R2.
Following is what I've tried.
SELECT DATEADD(hh, 03, DATEADD(mi, 30, DATEADD(ss, 00, DATEDIFF(dd, 0,GETDATE())))) as Customtime
Using the above query, I'm able to achieve it.
But is there any shorthand method already available to add custom time to datetime?
Solution 1:
Try this
SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), '03:30:00')
Solution 2:
For me, this code looks more explicit:
CAST(@SomeDate AS datetime) + CAST(@SomeTime AS datetime)
Solution 3:
Try this:
SELECT DATEDIFF(dd, 0,GETDATE()) + CONVERT(DATETIME,'03:30:00.000')
Solution 4:
Try this
SELECT DATEADD(MINUTE,HOW_MANY_MINUTES,TO_WHICH_TIME)
Here MINUTE
is constant which indicates er are going to add/subtract minutes from TO_WHICH_TIME
specifier. HOW_MANY_MINUTES
is the interval by which we need to add minutes, if it is specified negative, time will be subtracted, else would be added to the TO_WHICH_TIME
specifier and TO_WHICH_TIME
is the original time to which you are adding MINUTE
.
Hope this helps.