0

How can I combine a datetime value from two datetimes. The first value contains appropriate date and the second value contains appropriate time (but also has a date that I want to skip).

DECLARE @date DATETIME = '2016-12-08 00:00:00.000'
DECLARE @time DATETIME = '2016-12-07 12:15:00.000'

SELECT @date + @time returns: 2133-11-14 12:15:00.000 but I want: 2016-12-08 12:15:00.000

Paweł
  • 133
  • 2
  • 11
  • 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) – AHiggins Dec 07 '16 at 16:03
  • LukeH's answer on the referenced post should be what you need. – AHiggins Dec 07 '16 at 16:03

1 Answers1

1

Use style part in Convert function to extract date and time in varchar(avoids some explicit conversion to varchar) type and concat the the results to get the result date

  • 112 to extract date
  • 114 to extract time

Try this

DECLARE @date DATETIME = '2016-12-08 00:00:00.000'
DECLARE @time DATETIME = '2016-12-07 12:15:00.000'

SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), @date, 112) + ' '
                         + CONVERT(VARCHAR(50), @time, 114)) 

Result : 2016-12-08 12:15:00.000

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172