我需要计算作业的下一个月运行时间,可以使用两个可以从
获取值的参数来指定参数1:
1表示周日,2表示周一,3表示周三,4表示周三,5表示周四,6表示周六,8表示日,9表示平日,10表示周末
参数2:
1表示第一,2表示第二,4表示第三,8表示第四,16表示最后
根据这两个参数,您可以指定一个运行时间,例如该月的第一个工作日或该月的最后一个星期日。
如何使用将知道当前日期,para1和para2
的存储过程到达此日期答案 0 :(得分:1)
create function schema.get_next_run_date(@day int, @week int)
returns datetime
as
begin
declare @rtResult datetime
declare @frstDayOfMonth datetime
set @frstDayOfMonth = SELECT TRUNC(current_date, 'MM') FROM dual
if @day <= 7
set @rtResult = SELECT NEXT_DAY(@frstDayOfMonth + ((parm2-1)*7) day, param1) "NEXT DAY" FROM DUAL
else if @day = 8
set @rtResult = SELECT NEXT_DAY(@frstDayOfMonth + pamr2) "NEXT DAY" FROM DUAL
else if @day = 9
declare intDay int
set @intDay = datepart(weekday, @frstDayOfMonth)
if (@intDay between 2 and 6)
set @rtResult = @frstDayOfMonth
else if (@intDay = 1)
set @rtResult = @frstDayOfMonth + 1 day
else
set @rtResult = @frstDayOfMonth + 2 day
else if @day = 10
declare intDay int
set @intDay = datepart(weekday, @frstDayOfMonth)
if (@intDay between 2 and 6)
set @rtResult = @frstDayOfMonth + 7 - @intDay
else
set @rtResult = @frstDayOfMonth
else
set @rtResult = null
return @rtResult
end
go
未经测试。但我希望这会有所帮助。如果日期已经过期,您可能希望返回null。
答案 1 :(得分:1)
这可能会让你开始。取自:How-to-get-the-Nth-weekday-of-a-month
CREATE FUNCTION dbo.fnGetNthWeekdayOfMonth
(
@theDate DATETIME,
@theWeekday TINYINT,
@theNth SMALLINT
)
RETURNS DATETIME
BEGIN
RETURN (
SELECT theDate
FROM (
SELECT DATEADD(DAY, 7 * @theNth - 7 * SIGN(SIGN(@theNth) + 1) +(@theWeekday + 6 - DATEDIFF(DAY, '17530101', DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) % 7) % 7, DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) AS theDate
WHERE @theWeekday BETWEEN 1 AND 7
AND @theNth IN (-5, -4, -3, -2, -1, 1, 2, 3, 4, 5)
) AS d
WHERE DATEDIFF(MONTH, theDate, @theDate) = 0
)
END
这不完全是你想要的,但应涵盖主要部分。