简体   繁体   中英

sql database function (cut-off time on 4.30pm)

I'm try to write SQL Database function. It should count number of working day with a cut-off point at 4.30pm. So any orders before 4.30pm appear in the total for the day before, and after 4.30pm in the total for that day. I found the code that count number of working day but I know how to add the cut-off point into the code?

create or replace
function "number_of_worked_day"
(p_start_dt date,
 p_end_dt date
)
return number as
L_Number_Of_Days Number;
L_Start_Dt Date;
L_end_dt  DATE;

Begin
L_Start_Dt :=Trunc(P_Start_Dt);
L_end_dt  := trunc(p_end_dt);


SELECT COUNT(*) 
into l_number_of_days
FROM (
      WITH date_tab AS (SELECT TO_DATE (L_Start_Dt) + LEVEL - 1 business_date
      FROM DUAL
      CONNECT BY LEVEL <=
      TO_DATE (L_end_dt)
       - TO_DATE (L_Start_Dt)
       + 1)
SELECT business_date
FROM date_tab
WHERE TO_CHAR (business_date, 'DY') NOT IN ('SAT', 'SUN')
AND NOT EXISTS 
    ( SELECT 1
      FROM PUBLIC_HOLIDAY
      WHERE business_date between START_DT and END_DT));
return l_number_of_days;
end;

I find your code a bit hard to follow. But the logic for handling a day starting at 4:30 pm is to subtract 16.5 hours from the time. For instance:

SELECT trunc(business_date - 16.5/24), count(*)
FROM date_tab
GROUP BY trunc(business_date - 16.5/24)
ORDER BY trunc(business_date - 16.5/24);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM