时区更改sql Server中的CST和DST

时间:2014-03-11 14:58:19

标签: sql-server tsql sql-server-2012

SQL Server使用' datediff'返回的标准时间是多少?功能

datediff(ss, '1/1/1970', current_timestamp)

是GMT吗?

要求:

用于在CST和DST时间戳中返回时间戳的用户定义函数取决于当前时间(CST或白天节省灯)。

在CST时区i,该区域的CST时间比GMT低6小时'

(datediff(ss, '1/1/1970', current_timestamp) + 21600)

在夏令时(DST)期间,区域内的时间比GMT晚5个小时

(datediff(ss, '1/1/1970', current_timestamp) + 18000)

任何人都可以帮助我为上述要求创建用户定义的函数。

谢谢&问候, Venkat

1 个答案:

答案 0 :(得分:2)

不清楚你在问什么,但如果你想将UTC转换为CST,请尝试使用“-6”作为你的UTC偏移量。

---------------------------------------------------------------------------------------------------
--Name:     udfToLocalTime.sql
--Purpose:  To convert UTC to local US time accounting for DST
--Author:   Patrick Slesicki
--Date:     3/25/2014
--Notes:    Tested on SQL Server 2008R2 and later.
--          Good only for US States observing the Energy Policy Act of 2005.
--          Function doesn't apply for years prior to 2007.
--          Function assumes that the 1st day of the week is Sunday.
--Tests:        
--          SELECT dbo.udfToLocalTime('2014-03-09 9:00', DEFAULT)
--          SELECT dbo.udfToLocalTime('2014-03-09 10:00', DEFAULT)
--          SELECT dbo.udfToLocalTime('2014-11-02 8:00', DEFAULT)
--          SELECT dbo.udfToLocalTime('2014-11-02 9:00', DEFAULT)
---------------------------------------------------------------------------------------------------
ALTER FUNCTION udfToLocalTime
    (
    @UtcDateTime    AS DATETIME
    ,@UtcOffset     AS INT = -8 --PST
    )
RETURNS DATETIME
AS 
BEGIN
    DECLARE 
        @MyDateTime AS DATETIME
        ,@Year      AS CHAR(4)
        ,@MarTime   AS TIME
        ,@NovTime   AS TIME
        ,@Mar1      AS DATETIME
        ,@Nov1      AS DATETIME
        ,@Mar1Day   AS INT
        ,@Nov1Day   AS INT
        ,@MarDiff   AS INT
        ,@NovDiff   AS INT
        ,@DstStart  AS DATETIME
        ,@DstEnd    AS DATETIME

    SELECT
        @Year       = CONVERT(CHAR(4), YEAR(@UtcDateTime))
        ,@MarTime   = CONVERT(TIME, DATEADD(HOUR, -@UtcOffset, '1900-01-01 02:00'))
        ,@NovTime   = CONVERT(TIME, DATEADD(HOUR, -@UtcOffset - 1, '1900-01-01 02:00'))
        ,@Mar1      = CONVERT(CHAR(16), @Year + '-03-01 ' + CONVERT(CHAR(5), @MarTime), 126)
        ,@Nov1      = CONVERT(CHAR(16), @Year + '-11-01 ' + CONVERT(CHAR(5), @NovTime), 126)
        ,@Mar1Day   = DATEPART(WEEKDAY, @Mar1)
        ,@Nov1Day   = DATEPART(WEEKDAY, @Nov1)

    --Get number of days between Mar 1 and DST start date
    IF @Mar1Day = 1 SET @MarDiff = 7
    ELSE SET @MarDiff = 15 - @Mar1Day

    --Get number of days between Nov 1 and DST end date
    IF @Nov1Day = 1 SET @NovDiff = 0
    ELSE SET @NovDiff = 8 - @Nov1Day

    --Get DST start and end dates
    SELECT 
        @DstStart   = DATEADD(DAY, @MarDiff, @Mar1)
        ,@DstEnd    = DATEADD(DAY, @NovDiff, @Nov1)

    --Change UTC offset if @UtcDateTime is in DST Range
    IF @UtcDateTime >= @DstStart AND @UtcDateTime < @DstEnd SET @UtcOffset = @UtcOffset + 1

    --Get Conversion
    SET @MyDateTime = DATEADD(HOUR, @UtcOffset, @UtcDateTime)
    RETURN @MyDateTime
END
GO