Solved

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

Posted on 2008-10-09
4
1,158 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
4 Comments
 
LVL 73

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
database upgrade 8 72
grant user/role question 11 28
create a nested synonym 4 25
Oracle collections 15 17
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

785 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