0

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?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Krishna Thota
  • 6,646
  • 14
  • 54
  • 79

6 Answers6

9

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.

Community
  • 1
  • 1
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • This did not work from in 2012 and this actually changed the date to some new date – Avi Jul 25 '16 at 15:32
  • @hsanivag Considering this was tagged with SQL Server 2008 the answer was specific to that version. The SQL Fiddle link confirmed that it did work for that version. – Taryn Jul 25 '16 at 15:36
  • Please check the explanation here, this the code is working fine but for specific scenario and the links below will give you better explanation http://stackoverflow.com/a/700647/2634848 – Avi Jul 25 '16 at 15:46
  • @hsanivag I've updated the answer with a version that should work in 2012. – Taryn Jul 25 '16 at 15:54
0

This Worked For Me.

Select DateAdd(d, DateDiff(d, 0, Cast(OrderDate As datetime)), Cast(Delivery_Time as datetime))   From myTable
Krishna Thota
  • 6,646
  • 14
  • 54
  • 79
0
SELECT CAST(date_col AS DATETIME)+CAST(time_col AS DATETIME) FROM your_table

Tested on SQL SERVER 2012

Park Keeper
  • 520
  • 4
  • 6
  • Works fine but for specific scenario. Please check the below link for additional explanation http://stackoverflow.com/a/700647/2634848 – Avi Jul 25 '16 at 15:48
-1

Try this::

Select CONCAT(DATE(column_dateTIME),column_Time) from my Table
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
-1

For MySQL

Select CONCAT_WS(" ", date(dattime_columnname), time(time_column)) from table_name;

For SQL Server 2008:

select convert(varchar, datetime_column, 103) + ' ' + convert(varchar, time_column, 114) 
manish chavda
  • 19
  • 1
  • 3
-2

try this:

This should work in SQL SERVER

select CONVERT(varchar(10),dt_col,121)+' '+cast(t_col as varchar(20)) 
from your_table
Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58