我正在寻找:
是[Status.SchedFundingDate]在[今天]+[1个工作日]当天或之前
为此,我得到了:
convert(varchar(10), [Status.SchedFundingDate], 112) <= convert(varchar(10), getdate() + 1, 112)
我目前想弄清楚的问题是:
是[Status.SchedFundingDate]在[今天]+[1个日历日]当天或之前
CASE WHEN UPPER(DATENAME(DW, getdate())) = 'FRIDAY'
THEN convert(varchar(10), [Status.SchedFundingDate], 112) <= convert(varchar(10), getdate() + 3, 112)
ELSE convert(varchar(10), [Status.SchedFundingDate], 112) <= convert(varchar(10), getdate() + 1, 112)
END
上面的SQL语句在"<"符号。
( @@DateFirst + DatePart( weekday, YourDate ) - 1 ) % 7 + 1
将始终返回一个从1
到7
的整数,其中1
对应于周日,而不管DateFirst
或Language
的设置如何。
您可以在case
表达式中使用它,通过添加所需的天数来调整周末的日期:
-- Generate some sample data.
with Dates as (
select GetDate() as ADate, 1 as NumberOfDates
union all
select DateAdd( day, 1, ADate ), NumberOfDates + 1
from Dates
where NumberOfDates <= 14 )
-- Demonstrate adjusting weekend dates to the next Monday.
select ADate, DateName( weekday, ADate ) as Weekday,
DateAdd( day, case ( @@DateFirst + DatePart( weekday, ADate ) - 1 ) % 7 + 1
when 1 then 1 -- Sunday.
when 7 then 2 -- Saturday.
else 0 end, ADate ) as AdjustedADate
from Dates;
您不能在SELECT
上使用比较。SQL Server不知道什么是布尔值,必须使用BIT
。
例如,SELECT 1 < 0
无效,并给您相同的错误Incorrect syntax near '<'.
。
在SQL Server上,您必须执行以下操作:
SELECT CAST(CASE WHEN DATENAME(DW, GETDATE()) = 'Friday' AND [Status.SchedFundingDate] <= GETDATE() + 3
THEN 1
WHEN [Status.SchedFundingDate] <= GETDATE() + 1
THEN 1
ELSE 0
END AS BIT);