SQL中的相对日期计算算法

发布于 2025-01-08 19:25:25 字数 292 浏览 4 评论 0原文

我需要计算作业的下个月运行时间,可以使用两个参数指定,这两个参数可以从

参数 1 中获取值:
1 表示星期日,2 表示星期一,3 表示星期二,4 表示星期三,5 表示星期四,6 表示星期五,7 表示星期六,8 表示白天,9 表示工作日,10 表示周末

参数 2:
1 代表第一,2 代表第二,4 代表第三,8 代表第四,16 代表最后

根据这两个参数,您可以指定运行时间,例如该月的第一个工作日或该月的最后一个星期日。

如何使用知道当前日期、para1 和 para2 的存储过程得出此日期

I have a requirement to calculate the next monthly run time of a job which can be specified using two parameters which can take values from

Parameter 1:
1 for Sunday, 2 for Monday, 3 for Tuesday, 4 for Wednesday, 5 for Thursday, 6 for Friday, 7 for Saturday, 8 for Day, 9 for Weekday, 10 for Weekend day

Parameter 2:
1 for First, 2 for Second, 4 for Third, 8 for Fourth, 16 for Last

Based on these two parameters you can specify a run time like First weekday of the month or Last sunday of the month.

How do I arrive at this date using a stored procedure which will know current date, para1 and para2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若相惜即相离 2025-01-15 19:25:25
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。

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

not tested. but i hope this helps. And you may want to return null if the date is already overdue.

千寻… 2025-01-15 19:25:25

这可能会让你开始。摘自:如何获取一个月的第 N 个工作日

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

这不完全是您要寻找的内容,但应该涵盖主要部分。

This might get you started. Taken From: 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

It's not exactly what you are looking for, but should cover the main part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文