Solved

Oracle syntax for  Last four  business workings days

Posted on 2014-12-19
8
90 Views
Last Modified: 2014-12-19
Hi Experts

Can anyone help with the PL/SQL syntax to show dates that are within the last four business days on a where clause.

Many thanks!
0
Comment
Question by:MrDavidThorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40508903
as business days would vary depending on the business, country etc, the only real solution is to use a calendar table which, for each date, indicates if it is a business day (for you) or not.
starting from there (http://www.experts-exchange.com/Database/MS-SQL-Server/A_12267-Date-Fun-Part-One-Build-your-own-SQL-calendar-table-to-perform-complex-date-expressions.html), you would query like this:
with data as ( select PKDate , row_number() over ( order by PKDate  desc) rn from days  where PKDate  < trunc(sysdate) and is_workday = 1 ) select * from data where rn <= 4 

Open in new window

this is it about what you need to do, in short
0
 

Author Comment

by:MrDavidThorn
ID: 40508911
Well the Business week is Monday to Friday, so I want to avoid Saturday / Sunday
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40508923
is this the case for all your weeks? or will the official holidays not be skipped also?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Accepted Solution

by:
johnsone earned 250 total points
ID: 40508925
Assuming that you don't want to account for holidays, and only care about Saturday/Sunday, then this should work.
SELECT d 
FROM   (SELECT d 
        FROM   (SELECT Trunc(SYSDATE) - LEVEL + 1 d 
                FROM   dual 
                CONNECT BY LEVEL <= 7) 
        WHERE  Trim(To_char(d, 'Day')) NOT IN ( 'Saturday', 'Sunday' ) 
        ORDER  BY d) 
WHERE  ROWNUM <= 4; 

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40508928
ORDER  BY d)
should that not be :
ORDER  BY d desc)
0
 
LVL 35

Expert Comment

by:johnsone
ID: 40508930
Yes, I somehow missed that in the cut and paste.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40509265
Just to be clear, if today were Tuesday, you want a where clause that would select records with a date of yesterday, last Friday, Thursday, or Wednesday?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40509278
Or this (just subtract the holidays):
(  GREATEST ( NEXT_DAY ( v_start_date, 'MON') - v_start_date - 2, 0)
          + ((NEXT_DAY ( v_end_date, 'MON') - NEXT_DAY ( v_start_date, 'MON'))  / 7)  * 5)
          -  (GREATEST ( NEXT_DAY ( v_end_date, 'MON') - v_end_date - 3, 0))
          - Holidays

Open in new window

:p
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle Errors 11 87
TNS timeout - "Error-2147467259 ORA-12170: TNS Connect timeout occurred” 10 77
How to add an Index to a date/time field? 15 41
Shared Service Environment 2 54
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question