创建财政周数

create fiscal week number

如何将以下逻辑放入格式 SQL 服务器将用于创建财政周数?

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

CASE 语句应该有效:

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

您可以使用 case 表达式:

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

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

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

小于SQL Server 2012版本,您可以使用CASE