Search code examples
sql-servert-sqldatetimeconcatenationssms-2014

How to Combine to values into datetime


How to combine two values into a datetime field

 Example value 1 = 2010-10-26 00:00:00.000 (datetime)
 Example value 2 = 1650 (varchar)

 Desire Result 2010-10-26 16:50:00.00

Solution

  • You can add datetimes together in SQL Server. The trick is getting the time from the second one:

    select (value1 +
            cast(cast(stuff(value2, 3, 0, ':') as time) as datetime)
           )
    

    Alternatively, if value2 is an integer, you could do:

    select dateadd(minute,
                   (value2/100)*60 + value2%100,
                   value1
                  )