土曜日と日曜日を除く月の最後の 3 日間に等しいかどうか、現在の日付を確認したい...
月の最終日を取得できます
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
getdate()
土日を除く月の最後の 3 日間を確認する方法がわかりません。
とても役に立ちます..ありがとう
土曜日と日曜日を除く月の最後の 3 日間に等しいかどうか、現在の日付を確認したい...
月の最終日を取得できます
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
getdate()
土日を除く月の最後の 3 日間を確認する方法がわかりません。
とても役に立ちます..ありがとう
-- 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
あなたが本当に意味しているのが、その月の最後の3つの週末以外の日が必要であるということである場合、次のようになります。
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;
今日がこれらの3日間でない場合、これは結果をもたらしません。
これにより、土曜と日曜を除く月の最後の 3 日間も得られます。
SELECT 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) = '金曜日'