[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

calculate business days (sql, oracle)

How do I restrict a query results to a certain number of business days? For example, if I need something fort he past 15 business days.
0
teaone
Asked:
teaone
1 Solution
 
sdstuberCommented:
this will return the past 15 business days (meaning all days except Saturday and Sunday)
including today.


SELECT *
  FROM (SELECT *
          FROM (SELECT SYSDATE - LEVEL - 1 d
                  FROM DUAL
                CONNECT BY LEVEL <= 21)
         WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun')
        ORDER BY d DESC)
 WHERE ROWNUM <= 15

You could then use this to join to your other table

Alternatively you could generate a range...

SELECT min(d), max(d)
  FROM (SELECT *
          FROM (SELECT SYSDATE - LEVEL - 1 d
                  FROM DUAL
                CONNECT BY LEVEL <= 21)
         WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun')
        ORDER BY d DESC)
 WHERE ROWNUM <= 15;

and then use this where your table between those values; but then you'd also have to exlude Saturday and Sunday using a condition similar to that shown
0
 
teaoneAuthor Commented:
thank you!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now