marksmithy69
Programmer
Hello everyone. Is there an easy way to get the get the difference between 2 dates, but exclude weekends from the difference? Thanks again.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
CREATE OR REPLACE FUNCTION no_weekends(p_date1 IN DATE, p_date2 IN DATE) RETURN NUMBER IS
l_count NUMBER := 0;
l_date1 DATE := LEAST(p_date1, p_date2);
l_date2 DATE := GREATEST(p_date1, p_date2);
BEGIN
WHILE (l_date1 <= l_date2) LOOP
IF (TO_CHAR(l_date1,'DY') NOT IN ('SAT','SUN')) THEN
l_count := l_count + 1;
END IF;
l_date1 := l_date1 + 1;
END LOOP;
RETURN l_count;
END no_weekends;
/
SQL> select no_weekends('22-SEP-07','15-OCT-07') from dual;
NO_WEEKENDS('22-SEP-07','15-OCT-07')
------------------------------------
16
select count(*) from
(select to_char(to_date('22-sep-07', 'DD-MON-YY')+level, 'DY') as dy
from dual
connect by level <= (to_date('15-oct-07', 'DD-MON-YY') - to_date('22-sep-07', 'DD-MON-YY')))
where dy not in ('SAT', 'SUN')
create or replace function weekdays (dt1 date, dt2 date) return number is
weekday_count number;
begin
select count(*) into weekday_count
from (select rownum rn from all_tab_columns
where rownum <= greatest(dt1,dt2)-least(dt1,dt2))
where least(dt1,dt2)+rn <= greatest(dt1,dt2)
and to_char(least(dt1,dt2)+rn,'DY') not in ('SAT','SUN');
return weekday_count;
end;
/
Function created.
select weekdays('22-SEP-07','15-OCT-07') weekdays from dual;
WEEKDAYS
--------
16
create or replace function weekdays (dt1 date, dt2 date) return number is
weekday_count number;
begin
select (greatest(dt1,dt2)-least(dt1,dt2))-count(*) into weekday_count
from (select rownum rn from all_tab_columns
where rownum <= (greatest(dt1,dt2)-least(dt1,dt2))+1)
where trunc(least(dt1,dt2))+rn <= trunc(greatest(dt1,dt2))
and to_char(least(dt1,dt2)+rn,'DY') in ('SAT','SUN');
return weekday_count;
end;
/
Function created.
select weekdays(
to_date('6/8/2007 2:56:31 PM','mm/dd/yyyy hh:mi:ss pm'),
to_date('6/12/2007 12:23:23 PM','mm/dd/yyyy hh:mi:ss pm')
) diff
from dual;
DIFF
----------
1.89365741
*************************************************************************
create or replace function weekdays (dt1 date, dt2 date) return number is
weekday_count number;
beg_dt date := least(dt1,dt2);
end_dt date := greatest(dt1,dt2);
frags number;
begin
if trunc(end_dt) = trunc(beg_dt) then
if to_char(end_dt,'DY') in ('SAT','SUN') then return 0;
else return end_dt-beg_dt;
end if;
else
if to_char(beg_dt,'DY') in ('SAT','SUN') then frags := 0;
else frags := (trunc(beg_dt)+1) - beg_dt;
end if;
if to_char(end_dt,'DY') in ('SAT','SUN') then null;
else frags := frags + (end_dt - trunc(end_dt));
end if;
select frags+count(*) into weekday_count
from (select rownum rn from all_tab_columns
where rownum <= ((end_dt-beg_dt)+10))
where trunc(beg_dt)+rn <= trunc(trunc(end_dt)-(1/24/60/60))
and to_char(trunc(beg_dt)+rn,'DY') not in ('SAT','SUN');
return weekday_count;
end if;
end;
/
Function created.