Solved

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

Posted on 2008-10-09
4
1,156 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now