2

I have two columns as orderdate(03/02/2011) and ordertime(10.34 am) in which i have to concatenate these two columns and show it in another column as datetime values(03/02/2011 10:34:16.190)....

any suggestion?

bala3569
  • 10,832
  • 28
  • 102
  • 146
  • How did you get a time of `10:34:16.190` from an input of `10:34 AM`? – Thomas Feb 03 '11 at 06:45
  • 1
    possible duplicate of [How to combine date from one field with time from another field - MS SQL Server](http://stackoverflow.com/questions/700619/how-to-combine-date-from-one-field-with-time-from-another-field-ms-sql-server) – Lieven Keersmaekers Feb 03 '11 at 07:20

4 Answers4

3

In general, the solution is to add days to the time value which has zero for its day portion. You are not clear as the data types of the two values, however one solution would be:

With Inputs As
    (
    Select '20110302' As DateVal, '10:34 AM' As TimeVal
    )
Select DateAdd(d, DateDiff(d, 0, Cast(DateVal As datetime)), Cast(TimeVal as datetime))
From Inputs 

A more explicit version assuming the inputs are strings and given your exact inputs:

Set DateFormat MDY

With Inputs As
    (
    Select '03/02/2011' As DateVal, '10:34 AM' As TimeVal
    )
Select DateAdd(d, DateDiff(d, 0, Cast(DateVal As datetime)), Cast(TimeVal as datetime))
From Inputs
Thomas
  • 63,911
  • 12
  • 95
  • 141
2

Assuming you are working with a DATE and a TIME, you need to convert before you can add.

SELECT [orderdate] + CONVERT(datetime, [ordertime])
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
0

you want in programatically or in sql server:

in Sql Server:

select orderdate + ordertime as 'YourColumnName'

hope this help.

Suresh Chaudhary
  • 1,609
  • 5
  • 25
  • 40
0

In MySQL, it'll work like this

SELECT Concat(orderdate, " ", ordertime) FROM vendors ORDER BY vend_name;
Prabh
  • 2,466
  • 2
  • 24
  • 27