How do I exclude weekends in SQL?

Gayathri

I have a date column SLA_Date in the Orders table.

My SLA_Date should exclude weekends (Saturday & Sunday). Data for weekdays should be shown alone.

How do I do that in SQL?

Lalit Kumar B

You just need to add the following filter:

WHERE TO_CHAR(date_column, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN')

Your query would look like:

SELECT SLA_Date
   FROM orders
 WHERE TO_CHAR(SLA_Date, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN')

For example(the WITH clause is only to build a test case), the below query is to display only the weekdays(i.e. excluding the Sat and Sun) ranging from 1st May 2015 to 31st May 2015:

SQL> WITH DATA AS
  2    (SELECT to_date('05/01/2015', 'MM/DD/YYYY') date1,
  3      to_date('05/31/2015', 'MM/DD/YYYY') date2
  4    FROM dual
  5    )
  6  SELECT date1+LEVEL-1 the_date,
  7        TO_CHAR(date1+LEVEL-1, 'DY','NLS_DATE_LANGUAGE=AMERICAN') day
  8  FROM DATA
  9  WHERE TO_CHAR(date1+LEVEL-1, 'DY','NLS_DATE_LANGUAGE=AMERICAN')
 10        NOT IN ('SAT', 'SUN')
 11  CONNECT BY LEVEL <= date2-date1+1;

THE_DATE  DAY
--------- ---
01-MAY-15 FRI
04-MAY-15 MON
05-MAY-15 TUE
06-MAY-15 WED
07-MAY-15 THU
08-MAY-15 FRI
11-MAY-15 MON
12-MAY-15 TUE
13-MAY-15 WED
14-MAY-15 THU
15-MAY-15 FRI
18-MAY-15 MON
19-MAY-15 TUE
20-MAY-15 WED
21-MAY-15 THU
22-MAY-15 FRI
25-MAY-15 MON
26-MAY-15 TUE
27-MAY-15 WED
28-MAY-15 THU
29-MAY-15 FRI

21 rows selected.

SQL>

For a detailed example, see Generate Date, Month Name, Week Number, Day number between two dates

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I exclude weekends and holidays in SQL Server query

From Dev

SQL - How to exclude weekends from date range using 'datediff'

From Dev

Exclude / ignore weekends in Oracle SQL

From Dev

How do I exclude a specific record in SQL?

From Dev

How do I exclude Repositories in Spring tests?

From Dev

How do i exclude the whitespace matches in this regex?

From Dev

How do i exclude negative numbers in an if loop?

From Dev

Wildfly 9 - How do I exclude Jackson

From Dev

How do I exclude certain variable parts?

From Dev

How do I exclude search results in ActiveRecord?

From Dev

How do I exclude `.` and `..` from a directory listing

From Dev

How do i exclude negative numbers in an if loop?

From Dev

How do I exclude Repositories in Spring tests?

From Dev

Wildfly 9 - How do I exclude Jackson

From Dev

SQL - How do I exclude results from a table if they have the same data in two columns

From Dev

exclude weekends in javascript date calculation

From Dev

Exclude weekends days from the holidays

From Dev

Exclude weekends from my query

From Dev

How do I exclude multi-line set of data when ONE line of data has a specific value? SQL-ORACLE

From Dev

How do I ack-grep exclude file type *.sql files or file size larger than >3MB?

From Dev

SQL Server: I need to manipulate data differently on weekends than weekdays

From Dev

How do you exclude another table from a query in SQL Server?

From Dev

How do I zip up a folder but exclude the .git subfolder

From Dev

How do I exclude a package for a specific findbugs rule

From Dev

How do I use a variable to specify a path to exclude in `find`?

From Dev

How do I exclude files from git ls-files?

From Dev

How do I exclude files from karma code coverage report?

From Dev

How do I exclude the "require('react')" from my Browserified bundle?

From Dev

How do I exclude one category from a blog page on Wordpress?

Related Related

  1. 1

    How can I exclude weekends and holidays in SQL Server query

  2. 2

    SQL - How to exclude weekends from date range using 'datediff'

  3. 3

    Exclude / ignore weekends in Oracle SQL

  4. 4

    How do I exclude a specific record in SQL?

  5. 5

    How do I exclude Repositories in Spring tests?

  6. 6

    How do i exclude the whitespace matches in this regex?

  7. 7

    How do i exclude negative numbers in an if loop?

  8. 8

    Wildfly 9 - How do I exclude Jackson

  9. 9

    How do I exclude certain variable parts?

  10. 10

    How do I exclude search results in ActiveRecord?

  11. 11

    How do I exclude `.` and `..` from a directory listing

  12. 12

    How do i exclude negative numbers in an if loop?

  13. 13

    How do I exclude Repositories in Spring tests?

  14. 14

    Wildfly 9 - How do I exclude Jackson

  15. 15

    SQL - How do I exclude results from a table if they have the same data in two columns

  16. 16

    exclude weekends in javascript date calculation

  17. 17

    Exclude weekends days from the holidays

  18. 18

    Exclude weekends from my query

  19. 19

    How do I exclude multi-line set of data when ONE line of data has a specific value? SQL-ORACLE

  20. 20

    How do I ack-grep exclude file type *.sql files or file size larger than >3MB?

  21. 21

    SQL Server: I need to manipulate data differently on weekends than weekdays

  22. 22

    How do you exclude another table from a query in SQL Server?

  23. 23

    How do I zip up a folder but exclude the .git subfolder

  24. 24

    How do I exclude a package for a specific findbugs rule

  25. 25

    How do I use a variable to specify a path to exclude in `find`?

  26. 26

    How do I exclude files from git ls-files?

  27. 27

    How do I exclude files from karma code coverage report?

  28. 28

    How do I exclude the "require('react')" from my Browserified bundle?

  29. 29

    How do I exclude one category from a blog page on Wordpress?

HotTag

Archive