-1

I need to take a DATETIME column (Column A) that contains only a date, then add a VARCHAR(5) column (Column B) which contains a 24-hour representation of a time (i.e. 13:30) and combine them to get a valid DATETIME to be used in a DATEDIFF command.

So for example, Column A contains 2019-06-27 00:00:00.000 and Column B contains 13:30. The result should be 2019-06-27 13:30:00.000

JOhn
  • 1
  • 2
  • 2
    Possible duplicate of [How to combine date from one field with time from another field - MS SQL Server](https://stackoverflow.com/questions/700619/how-to-combine-date-from-one-field-with-time-from-another-field-ms-sql-server) – Amira Bedhiafi Jun 27 '19 at 08:47
  • What's wrong with `YourDatetimeColumn + YourVarcharTimeColumn`? – Thom A Jun 27 '19 at 08:57

1 Answers1

1

Cast ColumnA and ColumnB as datetime and concatenate the two columns.

DECLARE @TBL TABLE (ColumnA DATETIME, ColumnB TIME)
INSERT INTO @TBL VALUES ('2019-06-27 00:00:00.000','13:30')

SELECT
ColumnA,
ColumnB,
cast(ColumnA as datetime)+cast(ColumnB as datetime) AS NewColumn
FROM @TBL

Result

    ColumnA                    ColumnB                NewColumn
2019-06-27 00:00:00.000    13:30:00.0000000      2019-06-27 13:30:00.000
JonWay
  • 1,585
  • 17
  • 37