Question

  Asked: Feb 27 2008   9:48 PM GMT
  Asked by: OracleATE


Using SELECT statements


Oracle, Oracle development, SELECT statement

Can we get the number of saturdays in any month using a SELECT statement(i.e without creating a table or function)? If so, can you give me the logic?

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
+3
Click to Vote:
  •   3
  •  0



You can use a combination of oracle functions within one select statement to get your answer. The following code seems to work:

select (next_day(last_day(sysdate),'SAT') - next_day(last_day(add_months(sysdate,-1)),'SAT')) / 7 from dual;

Sysdate gives the current date; but you could replace sysdate with any date

The concept is to get the number of days between the first saturday of the month and the first saturday of the next month. Then divide by 7.

The first Saturday of NEXT month can be found by getting the last day of the current month <last_day(sysdate),> and then getting the next Saturday <next_day(last_day(sysdate),'SAT')>

To get the first Saturday of the CURRENT month, we first need to get the date one month back <add_months(sysdate,-1)>. Then we can get the last day of that month <last_day(add_months(sysdate,-1))>. Finally, we can get the first Saturday of the current month (which is the next Saturday after the last day of the previous month) <next_day(last_day(add_months(sysdate,-1)),'SAT')) >

Subtract those two dates and divide by 7 and you have your answer.
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Oracle and Database.

Looking for relevant Oracle Whitepapers? Visit the SearchOracle.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register