在sql server中将UTC转换为CET的功能

时间:2015-02-23 14:18:02

标签: sql sql-server

嗨,有人可以帮助我完成我的功能。我收到了这个错误

  

将值添加到' datetime2'列导致溢出。

这是我的功能:

CREATE FUNCTION [dbo].[udf_ConvertfromUTCtoCET] (@UTCDate AS DATETIME2(7))
RETURNS DATETIME2(7)
AS
BEGIN

DECLARE @DstStart datetime2(7)
DECLARE @DstEnd datetime2(7)
DECLARE @CetDate datetime2(7)

SELECT @DstStart = DATEADD(hour, 1,DATEADD(day, DATEDIFF(day, 0, '31/Mar' + CAST(YEAR(@UTCDate) AS varchar)) - 
        (DATEDIFF(day, 6, '31/Mar' + CAST(YEAR(@UTCDate) AS varchar)) % 7), 0)),
    @DstEnd = DATEADD(hour, 1,DATEADD(day, DATEDIFF(day, 0, '31/Oct' + CAST(YEAR(@UTCDate) AS varchar)) - 
        (DATEDIFF(day, 6, '31/Oct' + CAST(YEAR(@UTCDate) AS varchar)) % 7), 0))



SELECT @CetDate = CASE WHEN @UTCDate <= @DstEnd AND @UTCDate >= @DstStart
    THEN DATEADD(hour, +2, @UTCDate)
    ELSE DATEADD(hour, +1, @UTCDate) END

RETURN @CetDate
END

我在WarehoureMgmt.DimTime表格日期格式为datetime2(7),例如

9999-12-31 00:00:00.0000000
0001-01-01 00:00:00.0000000
2012-01-01 00:00:00.0000000
2012-01-01 01:00:00.0000000
2012-01-01 02:00:00.0000000

这就是我要选择的内容

select [DateTime],[dbo].[udf_ConvertfromUTCtoCET] ([DateTime]) 
from WarehouseMgmt.DimTime

1 个答案:

答案 0 :(得分:1)

问题可能是数据仓库中大于9999-12-31 23:00:00.0000000的值 - 增加1或2小时会导致溢出。如果你这样做

select max ([DateTime]) from WarehouseMgmt.DimTime

你会找到最大的值。