如果日期是sql server

时间:2015-08-21 14:10:07

标签: sql sql-server-2008 tsql

如果日期是t-SQL中if语句中的工作日,是否有更简洁的方法可以解决?

我想要比下面更简洁的东西

if ((DATENAME(WEEKDAY, @date) = 'Monday' or DATENAME(WEEKDAY, @date) = 'Tuesday' or DATENAME(WEEKDAY, @date) = 'Wednesday' or DATENAME(WEEKDAY, @date) = 'Thursday' or DATENAME(WEEKDAY, @date) = 'Friday'))
    --do something

3 个答案:

答案 0 :(得分:1)

如果您使用的是英国/美国标准DATEFIRST(默认)设置

使用WEEKDAY功能

CASE WHEN WEEKDAY(@date) BETWEEN 2 AND 6 THEN @dosomething ELSE 'WEEKEND' END

答案 1 :(得分:1)

你可以检查相反的情况:如果是周末那天。

select case when datename(weekday, getdate()) not in ('Saturday', 'Sunday') then 1 end weedayBoolean

答案 2 :(得分:0)

有点像以前的答案,但既不依赖语言也不依赖datefirst设置:

select
    CASE WHEN DATEPART(weekday,@date) in (
        DATEPART(weekday,'20150822'), --"Known good" Saturday
        DATEPART(weekday,'20150503') --"Known good" Sunday
    ) THEN 'Weekend'
    ELSE 'Weekday'
    END