Combining (concatenating) date and time into a datetime

后端 未结 12 926
夕颜
夕颜 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:07

    Concat date of one column with a time of another column in MySQL.

    SELECT CONVERT(concat(CONVERT('dateColumn',DATE),' ',CONVERT('timeColumn', TIME)), DATETIME) AS 'formattedDate' FROM dbs.tableName;
    
    0 讨论(0)
  • 2020-12-08 15:09

    The simple solution

    SELECT CAST(CollectionDate as DATETIME) + CAST(CollectionTime as DATETIME)
    FROM field
    
    0 讨论(0)
  • 2020-12-08 15:09

    Solution (1): datetime arithmetic

    Given @myDate, which can be anything that can be cast as a DATE, and @myTime, which can be anything that can be cast as a TIME, starting SQL Server 2014+ this works fine and does not involve string manipulation:

    CAST(CAST(@myDate as DATE) AS DATETIME) + CAST(CAST(@myTime as TIME) as DATETIME)
    

    You can verify with:

    SELECT  GETDATE(), 
            CAST(CAST(GETDATE() as DATE) AS DATETIME) + CAST(CAST(GETDATE() as TIME) as DATETIME)
    

    Solution (2): string manipulation

    SELECT  GETDATE(), 
            CONVERT(DATETIME, CONVERT(CHAR(8), GETDATE(), 112) + ' ' + CONVERT(CHAR(8), GETDATE(), 108))
    

    However, solution (1) is not only 2-3x faster than solution (2), it also preserves the microsecond part.

    See SQL Fiddle for the solution (1) using date arithmetic vs solution (2) involving string manipulation

    0 讨论(0)
  • 2020-12-08 15:11

    dealing with dates, dateadd must be used for precision

    declare @a DATE = getdate()
    declare @b time(7) = getdate()
    select @b, @A, GETDATE(), DATEADD(day, DATEDIFF(day, 0, @a), cast(@b as datetime2(0)))
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 15:16
    drop table test
    
    create table test(
        CollectionDate date NULL,
        CollectionTime  [time](0) NULL,
        CollectionDateTime as (isnull(convert(datetime,CollectionDate)+convert(datetime,CollectionTime),CollectionDate))
    -- if CollectionDate is datetime no need to convert it above
    )
    
    insert test (CollectionDate, CollectionTime)
    values ('2013-12-10', '22:51:19.227'),
           ('2013-12-10', null),
           (null, '22:51:19.227')
    
    select * from test
    
    CollectionDate  CollectionTime  CollectionDateTime
    2013-12-10      22:51:19        2013-12-10 22:51:19.000
    2013-12-10      NULL            2013-12-10 00:00:00.000
    NULL            22:51:19        NULL
    
    0 讨论(0)
提交回复
热议问题