Combining (concatenating) date and time into a datetime

后端 未结 12 977
夕颜
夕颜 2020-12-08 15:06

Using SQL Server 2008, this query works great:

select CAST(CollectionDate as DATE), CAST(CollectionTime as TIME)
from field

Gives me two co

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 15:12

    An easier solution (tested on SQL Server 2014 SP1 CU6)

    Code:

    DECLARE @Date date = SYSDATETIME();
    
    DECLARE @Time time(0) = SYSDATETIME();
    
    SELECT CAST(CONCAT(@Date, ' ', @Time) AS datetime2(0));
    

    This would also work given a table with a specific date and a specific time field. I use this method frequently given that we have vendor data that uses date and time in two separate fields.

提交回复
热议问题