我不是SQL服務器的專家,但這應該讓你關閉。把這個SQL函數:
DECLARE @date DATETIME = '10/03/2013';
SELECT MIN(Date) AS NextFridayOrEoMonth
FROM (SELECT DATEADD(DAY, (CASE DATEPART(DW, @date)
WHEN 7 THEN 6
ELSE 6 - DATEPART(DW, @date)
END), @date) AS Date
UNION
SELECT DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, @date) + 1, 0)) AS Date
) AS dates;
編輯:其實,在這裏它是作爲一種功能。祝你好運!
CREATE FUNCTION dbo.NextFridayOrEoMonth (@date DATETIME)
RETURNS DATETIME
WITH SCHEMABINDING,
RETURNS NULL ON NULL INPUT
AS
BEGIN
DECLARE @result DATETIME;
SELECT @result = MIN(Date)
FROM (SELECT DATEADD(DAY, (CASE DATEPART(DW, @date)
WHEN 7 THEN 6
ELSE 6 - DATEPART(DW, @date)
END), @date) AS Date
UNION
SELECT DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, @date) + 1, 0)) AS Date
) AS dates;
RETURN @result;
END
GO
SELECT dbo.NextFridayOrEoMonth('10/3/2013') AS NextFridayOrEoMonth; -- 2013-10-04
SELECT dbo.NextFridayOrEoMonth('10/5/2013') AS NextFridayOrEoMonth; -- 2013-10-11
SELECT dbo.NextFridayOrEoMonth('10/14/2013') AS NextFridayOrEoMonth; -- 2013-10-18
SELECT dbo.NextFridayOrEoMonth('10/25/2013') AS NextFridayOrEoMonth; -- 2013-10-25
SELECT dbo.NextFridayOrEoMonth('10/26/2013') AS NextFridayOrEoMonth; -- 2013-10-31
SELECT dbo.NextFridayOrEoMonth('10/29/2013') AS NextFridayOrEoMonth; -- 2013-10-31
GO
注:代碼審查/評論贊賞。
看看這一個.. http://stackoverflow.com/questions/5984704/sql-the-sp-or-function-should-calculate-the-next-date-for-friday – AJP
這只是找到下週五。 2013年10月26日將返還11/1/2013。需要它返回10/31/2013。不過謝謝。 – fsu1tm