Home » SQL & PL/SQL » SQL & PL/SQL » Connect by level date range - excluding weekends
Connect by level date range - excluding weekends [message #165213] Tue, 28 March 2006 23:29 Go to next message
danny_t
Messages: 52
Registered: March 2005
Member
I'm currently using:

SELECT <your_start_date>+rownum-1 thedate
  FROM ( SELECT 1
           FROM dual
         CONNECT BY level <= <your_end_date>-<your_start_date>+1
       )
/


to select a date range. Is it possible to use this but somehow exclude weekends?

Cheers,

Dan
Re: Connect by level date range - excluding weekends [message #165223 is a reply to message #165213] Wed, 29 March 2006 00:00 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
  1  select thedate
  2  ,      to_char(thedate, 'DY') dayname
  3  from   (SELECT to_date('01-04-2006', 'dd-mm-yyyy') + rownum - 1 thedate
  4          FROM   ( SELECT 1
  5                   FROM dual
  6                   CONNECT BY level <= to_date('15-04-2006', 'dd-mm-yyyy') - to_date('01-04-2006', 'dd-mm-yyyy') + 1
  7                 )
  8         )
  9* where  to_char(thedate, 'DY') not in ('SAT', 'SUN')
SQL> /

THEDATE    DAY
---------- ---
03-04-2006 MON
04-04-2006 TUE
05-04-2006 WED
06-04-2006 THU
07-04-2006 FRI
10-04-2006 MON
11-04-2006 TUE
12-04-2006 WED
13-04-2006 THU
14-04-2006 FRI

10 rows selected.


Instead of 'SAT' and 'SUN', you may need to use the abbreviations of your own language-settings.

hth
Re: Connect by level date range - excluding weekends [message #165238 is a reply to message #165223] Wed, 29 March 2006 00:47 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Frank wrote on Wed, 29 March 2006 08:00

Instead of 'SAT' and 'SUN', you may need to use the abbreviations of your own language-settings.
Or you could use the more general approach of the 'D' format mask: Sunday is day 1 and Saturday day 7. This works in any language:
SQL> select thedate
  2  ,      to_char(thedate, 'DY') dayname
  3  from   (SELECT to_date('01-04-2006', 'dd-mm-yyyy') + rownum - 1 thedate
  4          FROM   ( SELECT 1
  5                   FROM dual
  6                   CONNECT BY level <= to_date('15-04-2006', 'dd-mm-yyyy')
  7                                     - to_date('01-04-2006', 'dd-mm-yyyy') + 1
  8                 )
  9         )
 10  where  to_char(thedate, 'D') not in (7, 1)
 11  /

THEDATE   DAY
--------- ---
03-APR-06 MON
04-APR-06 TUE
05-APR-06 WED
06-APR-06 THU
07-APR-06 FRI
10-APR-06 MON
11-APR-06 TUE
12-APR-06 WED
13-APR-06 THU
14-APR-06 FRI

10 rows selected.


MHE
Re: Connect by level date range - excluding weekends [message #165246 is a reply to message #165238] Wed, 29 March 2006 00:59 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Maaher wrote

Or you could use the more general approach of the 'D' format mask: Sunday is day 1 and Saturday day 7. This works in any language:

Not true. Your nls-settings can also dictate whether sunday or monday is day 1 of the week.
The datetime format element D returns the number of the day of the week (1-7). The day of the week that is numbered 1 is specified implicitly by the initialization parameter NLS_TERRITORY.

[Edit: added link to docs]

[Updated on: Wed, 29 March 2006 01:05]

Report message to a moderator

Re: Connect by level date range - excluding weekends [message #165250 is a reply to message #165246] Wed, 29 March 2006 01:11 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Thanks for the correction and the link.

MHE
Previous Topic: Loading Data from XL sheets into oracle
Next Topic: how to compare rows within result produced into a single table
Goto Forum:
  


Current Time: Thu Aug 07 19:50:07 CDT 2025