Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-09
4
Medium Priority
?
1,176 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
2 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 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 1000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

876 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