Solved

CONNECT BY LEVEL <= MONTHS_BETWEEN stops at 10/31/08?

Posted on 2008-10-09
4
1,167 Views
Last Modified: 2012-05-05
The following code only creates months through 10/31/08, any clue why it tops and how to fix it?
SELECT     LAST_DAY (ADD_MONTHS (TRUNC (date '2008-01-01', 'MM'), 1 * LEVEL - 1)
                       ) MONTH
         FROM DUAL
   CONNECT BY LEVEL <= MONTHS_BETWEEN (date '2008-12-01', date '2008-01-01') + 1;

Open in new window

0
Comment
Question by:TwistYaWig
[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
4 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 250 total points
ID: 22684488
what version of Oracle?

early versions of 9i sometimes has problems with the connect by level from dual query due to a bug.  It's fixed in at least 9.2.0.6 but maybe earlier too.

try wrapping it in an inline view


select * from (
SELECT     LAST_DAY (ADD_MONTHS (TRUNC (date '2008-01-01', 'MM'), 1 * LEVEL - 1)
                       ) MONTH
         FROM DUAL
   CONNECT BY LEVEL <= MONTHS_BETWEEN (date '2008-12-01', date '2008-01-01') + 1);



you can also try modifying the query to see if the parser would figure it out better.

    SELECT   LAST_DAY(ADD_MONTHS(TRUNC(DATE '2008-01-01', 'MM'), 1 * LEVEL - 1)) month
      FROM   DUAL
CONNECT BY   ADD_MONTHS(TRUNC(DATE '2008-01-01', 'MM'), 1 * LEVEL - 1) <= DATE '2008-12-01';


the query "should" return the last day of every month in 2008 as is, if it doesn't there is a bug somewhere else, because the code is correct.


also, check if you are using some tool that only reads the first 10 rows of a result set by default.  If so, that might be your problem, since October is the 10th month it may appear
to be a problem in the query when really it could be the client.








0
 
LVL 27

Assisted Solution

by:sujith80
sujith80 earned 250 total points
ID: 22685657
Try this
SELECT     LAST_DAY (ADD_MONTHS (TRUNC (date '2008-01-01', 'MM'), 1 * X.lvl - 1) ) MONTH
FROM DUAL, 
(select level lvl from dual CONNECT BY LEVEL <= MONTHS_BETWEEN (date '2008-12-01', date '2008-01-01') + 1) X;
   

Open in new window

0

Featured Post

Independent Software Vendors: 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
Help on model clause 5 47
sum of columns in a row in oracle 3 42
looking for guidance on Oracle Sql Formatting standards 9 28
Oracle performance tuning 2 22
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to recover a database from a user managed backup

756 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