在我正在处理的提取中,我有2个datetime列。一列存储日期,另一列存储如下所示的时间。

如何查询表,将这两个字段组合成类型为datetime的1列?

日期

2009-03-12 00:00:00.000
2009-03-26 00:00:00.000
2009-03-26 00:00:00.000

1899-12-30 12:30:00.000
1899-12-30 10:00:00.000
1899-12-30 10:00:00.000

当前回答

简单地将两者连接起来,但首先将它们强制转换,如下所示

select cast(concat(Cast(DateField as varchar), ' ', Cast(TimeField as varchar)) as datetime) as DateWithTime from TableName;

其他回答

DECLARE @Dates table ([Date] datetime);
DECLARE @Times table ([Time] datetime);

INSERT INTO @Dates VALUES('2009-03-12 00:00:00.000');
INSERT INTO @Dates VALUES('2009-03-26 00:00:00.000');
INSERT INTO @Dates VALUES('2009-03-30 00:00:00.000');

INSERT INTO @Times VALUES('1899-12-30 12:30:00.000');
INSERT INTO @Times VALUES('1899-12-30 10:00:00.000');
INSERT INTO @Times VALUES('1899-12-30 10:00:00.000');

WITH Dates (ID, [Date])
AS (
    SELECT ROW_NUMBER() OVER (ORDER BY [Date]), [Date] FROM @Dates
), Times (ID, [Time])
AS (
    SELECT ROW_NUMBER() OVER (ORDER BY [Time]), [Time] FROM @Times
)
SELECT Dates.[Date] + Times.[Time] FROM Dates
    JOIN Times ON Times.ID = Dates.ID

打印:

2009-03-12 10:00:00.000
2009-03-26 10:00:00.000
2009-03-30 12:30:00.000
select s.SalesID from SalesTbl s 
        where cast(cast(s.SaleDate  as date) as datetime) + cast(cast(s.SaleCreatedDate as time) as datetime) between @FromDate and @ToDate

这是一个没有任何字符转换的替代解决方案:

DATEADD(ms, DATEDIFF(ms, '00:00:00', [Time]), CONVERT(DATETIME, [Date]))

这样你只能得到毫秒的精度,但这通常是可以的。我已经在SQL Server 2008中对此进行了测试。

如果日期列的时间元素和时间列的日期元素都为零,那么Lieven的答案就是您所需要的。如果你不能保证情况总是如此,那么事情就会变得稍微复杂一些:

SELECT DATEADD(day, 0, DATEDIFF(day, 0, your_date_column)) +
    DATEADD(day, 0 - DATEDIFF(day, 0, your_time_column), your_time_column)
FROM your_table

现有的答案不解决datetime2数据类型,所以我将添加我的:

假设你想添加一个时间值到datetime2值,其中:

datetime2值可以包含非零时间分量和/或小数秒 时间值可以包含值23:59:59.9999999,即86,399.9999999秒,86,399,999,999.9微秒或86,399,999,999,900纳秒¹

由于dateadd函数¹的限制,您必须分两步添加它们:

将时间值转换为秒并使用dateadd(second,…) 从时间值中提取纳秒,并使用dateadd(nanosecond,…)将它们添加到上面计算的日期中

declare @dv datetime2 = '2000-01-01 12:34:56.7890123';
declare @tv time = '23:59:59.9999999';
select dateadd(
    nanosecond,
    datepart(nanosecond, @tv),
    dateadd(
        second,
        datepart(hour, @tv) * 60 * 60 + datepart(minute, @tv) * 60 + datepart(second, @tv),
        @dv
    )
);
-- 2000-01-02 12:34:56.7890122

¹纳秒值可能不适合dateadd函数期望的int数据类型。