Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 254
Praveen Avatar asked Jul 03 '13 09:07

Praveen


2 Answers

Try this

SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), '03:30:00') 
like image 65
Aleksandr Fedorenko Avatar answered Sep 19 '22 11:09

Aleksandr Fedorenko


For me, this code looks more explicit:

CAST(@SomeDate AS datetime) + CAST(@SomeTime AS datetime) 
like image 44
Misha Avatar answered Sep 18 '22 11:09

Misha