有条件的WHERE语句显示某个财政周数

时间:2018-02-28 20:45:34

标签: sql-server-2014 where-clause case-statement

寻找有关如何最好地执行此代码的一些指导......

最后,我想将其插入到where子句中,以便根据当前日期驱动FSCL_YR_WK(即201804)。

AND A.FSCL_YR_WK =  Case 
                    WHEN GETDATE()  between 2018-02-26 AND 2018-03-04 THEN 201804 
                    WHEN GETDATE()  between 2018-03-05 AND 2018-03-11 THEN 201805
                    WHEN GETDATE()  between 2018-03-12 AND 2018-03-08 THEN 201806  
                ELSE '' END

1 个答案:

答案 0 :(得分:0)

丑陋但有效:

declare @d datetime
SET DATEFIRST 1--week starts on monday, default is sunday
set @d=convert(date,'2018-02-26')
select convert(char(4),datepart(year,@d))+RIGHT('0'+CAST(DATEPART( wk, @d)-5 AS VARCHAR(2)),2)

可能只是为了保持选择清除