Oracle (10g) 相当于 DATEADD(weekday, -3, GETDATE())

发布于 2024-11-02 03:22:18 字数 265 浏览 6 评论 0原文

我正在寻找相当于以下内容的 Oracle (10g):

DATEADD(weekday, -3, GETDATE())

from T-SQL (SQL Server) 。这从当前日期减去 3 个工作日。我不关心假期或类似的事情(我可以​​自己截断时间部分)。只要排除周末就可以了。

I'm looking for the Oracle (10g) equivalent of:

DATEADD(weekday, -3, GETDATE())

from T-SQL (SQL Server) . This subtracts 3 weekdays from the current date. I'm not concerned about holidays or anything like that (and I can truncate the time part off myself). Just excluding weekends is fine.

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

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

发布评论

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

评论(2

我很OK 2024-11-09 03:22:18

无需 PL/SQL 函数即可完成。只需根据星期几减去不同的天数:

select trunc(sysdate) - case to_char(sysdate, 'D')
                        when '4' then 3 -- thursday minus 3 days
                        when '5' then 3 -- friday minus 3 days
                        when '6' then 4 -- saturday minus 4 days
                        else 5          -- all other days minus 5 days
                        end
from dual;

当您必须在例如 12 天前执行此操作时,它看起来像:

select trunc(sysdate) - case to_char(sysdate, 'D')
                        when '1' then 18 -- mondays minus 18 days (incl. 3 weekends)
                        when '2' then 18 -- tuesdays minus 18 days (incl. 3 weekends)
                        when '6' then 17 -- saturdays minus 17 days (incl. 2 weekends and a saturday)
                        else 16          -- all others minus 16 days (incl. 2 weekends)
                        end
from dual;

请注意,星期几取决于数据库的 NLS_TERRITORY(在美国,第 1 天)是星期日,在大多数其他情况下,第一天是星期一)。

It can be done without a PL/SQL function. Just subtract a different number of days depending on the day of the week:

select trunc(sysdate) - case to_char(sysdate, 'D')
                        when '4' then 3 -- thursday minus 3 days
                        when '5' then 3 -- friday minus 3 days
                        when '6' then 4 -- saturday minus 4 days
                        else 5          -- all other days minus 5 days
                        end
from dual;

When you have to do this for e.g. 12 days back, it would look like:

select trunc(sysdate) - case to_char(sysdate, 'D')
                        when '1' then 18 -- mondays minus 18 days (incl. 3 weekends)
                        when '2' then 18 -- tuesdays minus 18 days (incl. 3 weekends)
                        when '6' then 17 -- saturdays minus 17 days (incl. 2 weekends and a saturday)
                        else 16          -- all others minus 16 days (incl. 2 weekends)
                        end
from dual;

Please note that day of week depends on the NLS_TERRITORY of your database (in America day 1 is Sunday, in most others day 1 is Monday).

夜唯美灬不弃 2024-11-09 03:22:18

看起来您需要创建一个 UDF。

CREATE OR REPLACE FUNCTION business_date (start_date DATE, 
days2add NUMBER) RETURN DATE IS
 Counter NATURAL := 0;
 CurDate DATE := start_date;
 DayNum POSITIVE;
 SkipCntr NATURAL := 0;
 Direction INTEGER := 1;  -- days after start_date
 BusinessDays NUMBER := Days2Add;
BEGIN
  IF Days2Add < 0 THEN
    Direction := - 1; -- days before start_date
    BusinessDays := (-1) * BusinessDays;
  END IF;

  WHILE Counter < BusinessDays LOOP
    CurDate := CurDate + Direction;
    DayNum := TO_CHAR( CurDate, 'D');

    IF DayNum BETWEEN 2 AND 6 THEN
      Counter := Counter + 1;
    ELSE
      SkipCntr := SkipCntr + 1;
    END IF;
  END LOOP;

  RETURN start_date + (Direction * (Counter + SkipCntr));
END business_date;

由 Larry Benton 提供,来自此处

It looks like you need to create a UDF.

CREATE OR REPLACE FUNCTION business_date (start_date DATE, 
days2add NUMBER) RETURN DATE IS
 Counter NATURAL := 0;
 CurDate DATE := start_date;
 DayNum POSITIVE;
 SkipCntr NATURAL := 0;
 Direction INTEGER := 1;  -- days after start_date
 BusinessDays NUMBER := Days2Add;
BEGIN
  IF Days2Add < 0 THEN
    Direction := - 1; -- days before start_date
    BusinessDays := (-1) * BusinessDays;
  END IF;

  WHILE Counter < BusinessDays LOOP
    CurDate := CurDate + Direction;
    DayNum := TO_CHAR( CurDate, 'D');

    IF DayNum BETWEEN 2 AND 6 THEN
      Counter := Counter + 1;
    ELSE
      SkipCntr := SkipCntr + 1;
    END IF;
  END LOOP;

  RETURN start_date + (Direction * (Counter + SkipCntr));
END business_date;

Courtesy of Larry Benton, from here.

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