如何向日期添加给定天数并排除周末

问题描述 投票:0回答:3

如何向日期字段添加天数并跳过周末的计算。

ex: if  Date  = 7/7/2021  then  Due Date = 07/21/2021`

Date = uxsubmtlDateRcvdDOP,
Number of days = 10, 
Due Date = uxsubmtlDateRcvdDOP + 10`

我有

Select uxsubmtlDateRcvdDOP + 10 as duedate

但这也计算周末。

sql oracle
3个回答
2
投票

这可以在 Oracle 中使用一个小型 PL/SQL 函数来完成,该函数使用

TRUNC(<date>, 'IW')
来查找包含您的日期的一周中的星期一。

create or replace function plus_weekdays(d0 date, weekdays number) return date
is
  d0_Monday            date;    /* Monday <= d0 */
  weekdays_from_Monday number;  /* # of weekdays to increment from d0_Monday */
  nweeks               int;     /* # of weeks to increment from d0_Monday */
  ndays                number;  /* # of days to increment after nweeks */
begin
  d0_Monday := trunc(d0, 'iw');

  if d0 - d0_Monday >= 5 then
    -- This case means the given date is a Saturday or Sunday.
    -- We round the given date up to the next Monday and decerement the 
    -- number of weekdays that we are adding. For example:
    --   Saturday - 1 = Thursday (computed as Monday - 2)
    --   Saturday + 0 = Friday   (computed as Monday - 1)
    --   Saturday + 1 = Monday   (computed as Monday + 0)
    --   Saturday + 2 = Tuesday  (computed as Monday + 1)

    d0_Monday := d0_Monday + 7;
    weekdays_from_Monday := weekdays - 1;
  else
    weekdays_from_Monday := weekdays + (d0 - d0_Monday);
  end if;

  nweeks := floor(weekdays_from_Monday / 5);
  ndays  := (weekdays_from_Monday - nweeks * 5);

  return d0_Monday + nweeks * 7 + ndays;
end;
/
show errors;

SQL> select plus_weekdays(DATE '2020-07-07', 10) from dual;

PLUS_WEEK                                                                       
---------                                                                       
21-JUL-20                                                                       

1
投票

您可以使用:

SELECT date_value,
       added_weekdays,
       week_start
       + MOD(days_from_week_start, 5)        -- Number of days of part week
       + FLOOR(days_from_week_start / 5) * 7 -- Number of full weeks
         AS final_value
FROM   (
  SELECT date_value,
         added_weekdays,
         TRUNC(date_value + 2, 'IW') AS week_start,
         CASE
         WHEN date_value - TRUNC(date_value, 'IW') >= 5
         THEN added_weekdays
         ELSE added_weekdays + date_value - TRUNC(date_value, 'IW')
         END AS days_from_week_start
  FROM   table_name t
)

对于样本数据:

CREATE TABLE table_name ( date_value, added_weekdays ) AS
SELECT DATE '2021-07-07', 10 FROM DUAL UNION ALL
SELECT DATE '2021-07-07', 11 FROM DUAL UNION ALL
SELECT DATE '2021-07-07', 12 FROM DUAL UNION ALL
SELECT DATE '2021-07-07', 13 FROM DUAL UNION ALL
SELECT DATE '2021-07-09',  3 FROM DUAL UNION ALL
SELECT DATE '2021-07-10',  0 FROM DUAL UNION ALL
SELECT DATE '2021-07-10',  1 FROM DUAL UNION ALL
SELECT DATE '2021-07-10' - INTERVAL '1' SECOND,  1/86400 FROM DUAL;

输出(日期格式为

YYYY-MM-DD HH24:MI:SS(DY)
):

DATE_VALUE ADDED_WEEKDAYS FINAL_VALUE
2021-07-07 00:00:00(周三) 10 2021-07-21 00:00:00(周三)
2021-07-07 00:00:00(周三) 11 2021-07-22 00:00:00 (周四)
2021-07-07 00:00:00(周三) 12 2021-07-23 00:00:00(周五)
2021-07-07 00:00:00(周三) 13 2021-07-26 00:00:00(周一)
2021-07-09 00:00:00(周五) 3 2021-07-14 00:00:00(周三)
2021-07-10 00:00:00 (周六) 0 2021-07-12 00:00:00(周一)
2021-07-10 00:00:00 (周六) 1 2021-07-13 00:00:00(星期二)
2021-07-09 23:59:59(周五) .000011574074 2021-07-12 00:00:00(周一)

小提琴


-1
投票
Dateadd(mydate,,trunc(mydays/5)*7 + mod(mydays,5) + 2*trunc((weekday(mydate)+mod(mydays,5))/7))

最后一个学期检查您是否跨越了最后一个周末的界限。

如果您的开始日期是星期六或星期日,这有点奇怪。 星期五 + 1 天 = 星期一,星期六 + 1 天 = 星期二,但星期日 + 1 天是星期一。

© www.soinside.com 2019 - 2024. All rights reserved.