Krishna Thota Krishna Thota - 4 months ago 10
SQL Question

Concatenate Date and Time Variable using SQL Query

I have two Columns in my SQL Table. One is DateTime and The other is Time
They are in the Format,

DateTime - 2012-04-05 16:58:56.000
and Time -
21:30:00.00000


I need to write a Query which will return a Column as -
2012-04-05 21:30:00.000


Can you Help me?

Answer

In SQL-Server 2008, you can add the values this way:

select cast(dt as date) + cast(tm as datetime)
from yourtable

See SQL Fiddle with Demo

The above no longer works, here is an updated version that will work in SQL Server. This can also be edited to include additional precision:

SELECT dateadd(day, datediff(day,'19000101',dt), CAST(tm AS DATETIME))
from yourtable

See Demo.

Comments