我正在建立一个日期维度,我需要做的最后一部分是填充一个标志,该标志指出该日期是否在英国夏令时之内。英国夏令时从3月的最后一个星期日开始,到10月的最后一个星期日结束。到目前为止,我已经知道了:
update [Dim_Date_Test_BST]
set IsBST = 1
where (CalenderMonth BETWEEN 03 and 10)
我还有以下代码,列出了每个月的最后一个星期日:
dateadd(dd,(datediff(dd,'17530107',dateadd(mm,datediff(mm,-1,Date),-1))/7)*7,'17530107')
我正在努力将这两段代码结合起来以产生所需的结果。有任何想法吗?谢谢。
修复,感谢以下贡献者:
SELECT DISTINCT
CalenderYear = D.CalenderYear,
BritishSummerStartDate =
CASE
WHEN D.CalenderMonth = 3
THEN dateadd(dd,(datediff(dd,'17530107',dateadd(mm,datediff(mm,-1,D.Date),-1))/7)*7,'17530107')
END,
BritishSummerEndDate = CASE
WHEN D.CalenderMonth = 10
THEN dateadd(dd,(datediff(dd,'17530107',dateadd(mm,datediff(mm,-1,D.Date),-1))/7)*7,'17530107')
END
INTO #BST
FROM [dbo].[Dim_Date_Test_BST] AS D
WHERE D.CalenderMonth IN (3, 10)
update D
SET IsBST = 1
FROM[dbo].[Dim_Date_Test_BST] AS D
INNER JOIN #BST a ON d.CalenderYear = a.CalenderYear AND a.BritishSummerStartDate IS NOT NULL
INNER JOIN #BST b ON d.CalenderYear = b.CalenderYear AND b.BritishSummerEndDate IS NOT NULL
WHERE D.Date BETWEEN a.BritishSummerStartDate AND b.BritishSummerEndDate
答案 0 :(得分:2)
您可以尝试每年生成一个具有正确开始/结束时间的集合,然后将其用作参考BETWEEN
。
;WITH BritishSummerPeriodsByYear AS
(
SELECT DISTINCT
CalenderYear = D.CalenderYear,
BritishSummerStartDate = CASE
WHEN D.CalenderMonth = 3
THEN dateadd(dd,(datediff(dd,'17530107',dateadd(mm,datediff(mm,-1,D.Date),-1))/7)*7,'17530107') END,
BritishSummerEndDate = CASE
WHEN D.CalenderMonth = 10
THEN dateadd(dd,(datediff(dd,'17530107',dateadd(mm,datediff(mm,-1,D.Date),-1))/7)*7,'17530107') END
FROM
[Dim_Date_Test_BST] AS D
WHERE
D.CalenderMonth IN (3, 10)
)
update D SET
IsBST = 1
FROM
Dim_Date_Test_BST AS D
INNER JOIN BritishSummerPeriodsByYear AS BP ON D.CalenderYear = BP.CalenderYear
WHERE
D.Date BETWEEN BP.BritishSummerStartDate AND BP.BritishSummerEndDate
答案 1 :(得分:0)
有一个更简单的方法,根据我们是否在 BST 中返回 1 或 0。
SUBSTRING(REVERSE(<your date> AT TIME ZONE 'GMT Standard Time'), 4,1) AS IsBST