我要檢查當前的日期,如果它等於上個月不包括週六和週日三天......檢查當前日期與月份的最後三天
我可以得到一個月的最後一天
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;
這會沒有結果,如果今天不是在那三天。
非常感謝..幫助 – user1046415 2012-08-06 15:30:41
這也使得本月不包括週六和週日
SELECT LastFriDay,LastFriDay-1 LastThursday,LastFriDay-2 LastWednesday FROM( SELECT DATEADD(毫米,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 () - DAY(GETDATE())+1)-3 AS LastFriDay UNION ALL SELECT DATEADD(mm,1,GETDATE() - DAY(GETDATE())+1)-4 AS LastFriDay UNION ALL(mm,1,GETDATE() - DAY(GETDATE())+ 1) - 選擇DATEADD(mm,1,GETDATE() - DAY(GETDATE())+ 1)-5 AS LastFriDay UNION ALL 6 AS LastFriDay UNION ALL SELECT DATEADD(毫米,1,GETDATE() - DAY(GETDATE())+ 1)-7 AS LastFriDay )AS YourFridayTable WHERE DATENAME(星期,LastFriDay)= '星期五'
那不是本月的最後一天。這是本月的最後一秒,如果將它分配給smalldatetime變量,它將會四捨五入,並且如果將它與datetime2進行比較,它可能會丟失數據。爲什麼這個標記爲Oracle? – 2012-08-03 20:25:23
你也可以明確地確認你的意思是「不包括週六和週日」嗎?你的意思是如果這個月的最後一天是星期六,你實際上是想包括星期三/星期五/星期五而不是星期四/星期五/星期六? – 2012-08-03 20:32:08