创建财政周数

时间:2017-06-14 15:55:00

标签: sql sql-server fiscal

如何将以下逻辑放入SQL Server用于创建会计周数的格式中?

if (datepart(week,getdate())-4) <= 0  then (datepart(week,getdate())+49) else (datepart(week,getdate())-4)

3 个答案:

答案 0 :(得分:1)

CASE声明应该有效:

CASE 
    WHEN datepart(week,getdate())-4) <= 0
        THEN datepart(week,getdate())+49
    ELSE
        datepart(week,getdate())-4
END

答案 1 :(得分:0)

您可以使用case表达式:

SELECT CASE WHEN (DATEPART(WEEK, getdate()) - 4) <= 0
            THEN DATEPART(WEEK, getdate()) + 49
            ELSE DATEPART(WEEK, getdate()) - 4
       END

答案 2 :(得分:0)

对于SQL Server 2012+,您可以使用IIF

SELECT IIF((datepart(week, getdate())-4) <= 0, datepart(week, getdate()) + 49, datepart(week, getdate()) - 4 )

对于少于SQL Server 2012的版本,您可以使用CASE