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?

0

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This did not work from in 2012 and this actually changed the date to some new date
@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.
Please check the explanation here, this the code is working fine but for specific scenario and the links below will give you better explanation stackoverflow.com/a/700647/2634848
@hsanivag I've updated the answer with a version that should work in 2012.
0

This Worked For Me.

Select DateAdd(d, DateDiff(d, 0, Cast(OrderDate As datetime)), Cast(Delivery_Time as datetime))   From myTable

Comments

0
SELECT CAST(date_col AS DATETIME)+CAST(time_col AS DATETIME) FROM your_table

Tested on SQL SERVER 2012

1 Comment

Works fine but for specific scenario. Please check the below link for additional explanation stackoverflow.com/a/700647/2634848
-1

Try this::

Select CONCAT(DATE(column_dateTIME),column_Time) from my Table

2 Comments

It Returns 'DATE' is not a recognized built-in function name.
@SashiKant The question is about sql-server, I have retagged it as such already. How is your sql-server kung fu Sashi?
-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) 

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.