我想检查当前日期是否等于一个月的最后三天,不包括周六和周日......
我可以得到一个月的最后一天
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
我不知道如何检查getdate()
每月的最后三天,不包括周六和周日..
会有很大帮助..谢谢
我想检查当前日期是否等于一个月的最后三天,不包括周六和周日......
我可以得到一个月的最后一天
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
我不知道如何检查getdate()
每月的最后三天,不包括周六和周日..
会有很大帮助..谢谢
-- make sure Sunday is the first day of the week:
SET DATEFIRST 7;
DECLARE
@today SMALLDATETIME,
@nextmonth SMALLDATETIME;
-- today at midnight is the number of days since 1900-01-01
-- the first day of nextmonth is one month after the first
-- day of the current month
SELECT
@today = DATEDIFF(DAY, '19000101', CURRENT_TIMESTAMP),
@nextmonth = DATEADD(MONTH, 1, @today-DAY(@today)+1);
-- so if today is greater than 3 days prior to the first of next month
-- and today is not a Saturday or a Sunday:
IF @today >= DATEADD(DAY, -3, @nextmonth)
AND DATEPART(WEEKDAY, @today) NOT IN (1,7)
BEGIN
PRINT 'Bazinga!';
END
如果您的真正意思是您想要该月的最后三个非周末,那么:
SET DATEFIRST 7;
DECLARE
@today SMALLDATETIME,
@nextmonth SMALLDATETIME;
SELECT
@today = DATEDIFF(DAY, 0, GETDATE()),
@nextmonth = DATEADD(MONTH, 1, @today-DAY(@today)+1);
;WITH x AS
(
-- get the 5 days prior to the first day of next month:
SELECT TOP (5) d = DATEADD(DAY, -ROW_NUMBER() OVER
(ORDER BY [object_id]), @nextmonth) FROM sys.all_objects
),
y AS
(
-- get the last three days that aren't on Sat/Sun:
SELECT TOP (3) d FROM x
WHERE DATEPART(WEEKDAY, d) NOT IN (1,7)
ORDER BY d DESC
)
-- now find out if today is in those three days:
-- (by definition, today can't be Sat/Sun)
SELECT 'Bazinga!' FROM y WHERE d = @today;
如果今天不在这三天内,这将不会产生任何结果。
这也给出了一个月的最后三天,不包括周六和周日
选择 LastFriDay,LastFriDay-1 LastThursday,LastFriDay-2 LastWednesday FROM ( SELECT DATEADD(mm,1,GETDATE() - DAY(GETDATE())+1)-1 AS LastFriDay UNION ALL SELECT DATEADD(mm,1,GETDATE() - DAY(GETDATE())+1)-2 AS LastFriDay UNION ALL SELECT DATEADD(mm,1,GETDATE() - DAY(GETDATE())+1)-3 AS LastFriDay UNION ALL SELECT DATEADD(mm,1,GETDATE () - DAY(GETDATE())+1)-4 AS LastFriDay UNION ALL SELECT DATEADD(mm,1,GETDATE() - DAY(GETDATE())+1)-5 AS LastFriDay UNION ALL SELECT DATEADD(mm,1 ,GETDATE() - DAY(GETDATE())+1)-6 AS LastFriDay UNION ALL SELECT DATEADD(mm,1,GETDATE() - DAY(GETDATE())+1)-7 AS LastFriDay ) AS YourFridayTable WHERE DATENAME( WeekDay,LastFriDay) = '星期五'