Thread: Creating Dates without CONNECT BY


Permlink Replies: 36 - Pages: 3 [ 1 2 3 | Next ] - Last Post: Oct 8, 2008 9:20 AM Last Post By: Stewart W. Bryson
user575545

Posts: 90
Registered: 05/20/07
Creating Dates without CONNECT BY
Posted: Jun 25, 2008 6:58 AM
Click to report abuse...   Click to reply to this thread Reply
How can I create dates between date range i.e if user enters 10-jun-2008 and 20-jun-2008,query should return dates from 10-jun to 20-jun (10 dates).
without using CONNECT BY .
Keith Jamieson

Posts: 2,132
Registered: 12/30/05
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:12 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
Err, thats 11 dates assuming you want both dates supplied.

select 
to_date('10-jun-2008','dd-mon-yyyy')+(rownum - 1)
from all_objects where rownum <=
( to_date('20-jun-2008','dd-mon-yyyy') - to_date('10-jun-2008','dd-mon-yyyy'))+1
R.Subramanian

Posts: 566
Registered: 02/14/07
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:13 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
Why?

You could do it by querying from all_objects (provided you have that many objects in the database.

Regards

Raj

[Edit:] I need to improve my typing speed :)

Message was edited by:
R.Subramanian
Chaitanya.S.S.K

Posts: 1,252
Registered: 12/28/05
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:17 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
one way:


SQL> with t as (
  2  select to_date('10-jun-2008') min_date, to_date('20-jun-2008') max_date from dual)
  3  --
  4  select min_date+ rn from (
  5  select rownum rn from user_objects,t where rownum<=max_date-min_date),t;
 
MIN_DATE+
---------
11-JUN-08
12-JUN-08
13-JUN-08
14-JUN-08
15-JUN-08
16-JUN-08
17-JUN-08
18-JUN-08
19-JUN-08
20-JUN-08
 
10 rows selected.
 


But it is limited to the maximum number of rows in the user_objects view.
user575545

Posts: 90
Registered: 05/20/07
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:18 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
thanks for replies. Just wondering I've never heard of all_objects ,what does it for?
Chaitanya.S.S.K

Posts: 1,252
Registered: 12/28/05
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:20 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
You can read about it:

http://www.oracle.com/pls/db102/homepage?remark=tahiti

(The search field is very handy)
R.Subramanian

Posts: 566
Registered: 02/14/07
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:21 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
Keith Jamieson

Posts: 2,132
Registered: 12/30/05
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:26 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
all_objects is a view in the oracle data dictionary.

The view contains details of all objects that you have access to, whether or not you own them, 1 entry per object.
It is preferable to use all_objects rather than user_objects as user_objects only contains the objects which you own.

example: in my test schema user_objects contains 240 rows
all_objects contains 58,750 rows.

so if you use user_objects here you will never get more than 240 rows with my query on my database.

If you use all_objects, then you will never get more than 58,750 rows on my database
user575545

Posts: 90
Registered: 05/20/07
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:32 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
ok ,I understand now about all_objects, but how is it helping me in this query:

select to_date('10-jun-2008','dd-mon-yyyy')+(rownum - 1)from all_objects
where rownum <= ( to_date('20-jun-2008','dd-mon-yyyy') - to_date('10-jun-2008','dd-mon-yyyy'))+1
Keith Jamieson

Posts: 2,132
Registered: 12/30/05
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:43 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
Try using dual with the same parameters and see what happens.

for each row returned, a day is added. If a row is not returned no more days will be added.

Any table can be used but it must have the number of days in your date_range.
all_objects will usually be sufficient, in this case.
user575545

Posts: 90
Registered: 05/20/07
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:50 AM   in response to: Keith Jamieson in response to: Keith Jamieson
Click to report abuse...   Click to reply to this thread Reply
so all_objects hold rownum as an object or number of days ?
Boneist

Posts: 3,496
Registered: 04/26/07
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:54 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
rownum is just an internal counter that Oracle keeps. You should consider looking in the documentation: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns009.htm#SQLRF00255

The query is using it to work out what to add on to the base date (eg. 10th June) in order to generate the dates.

Eg. 10th June + 0 = 10th June
10th June + 1 = 11th June
...
10th June + 10 = 20th June

Message was edited by:
Boneist for clarity
Frank Kulash

Posts: 7,653
Registered: 03/27/08
ROWNUM
Posted: Jun 25, 2008 7:56 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
Hi,

ROWNUM is a pseudo-column which you can select from any table or view. Its data type is NUMBER.
You can find out all about it in the SQL Language manual:
http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/pseudocolumns009.htm#sthref834
Keith Jamieson

Posts: 2,132
Registered: 12/30/05
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 7:58 AM   in response to: user575545 in response to: user575545
Click to report abuse...   Click to reply to this thread Reply
No, rownum is nothing to do with all_objects.

rownum is a pseudovariable which increments by 1 for every row retrieved. It doesn't exist in any table or view.

It is being used to add 1 day to the date for each row that is returned.
Thats why you need to have at least the number of rows that match your number of days. your result will be incorrect if this is not the case
user575545

Posts: 90
Registered: 05/20/07
Re: Creating Dates without CONNECT BY
Posted: Jun 25, 2008 8:01 AM   in response to: Keith Jamieson in response to: Keith Jamieson
Click to report abuse...   Click to reply to this thread Reply
I completely understand what rownum does, my question is why all_objects ,why not dual ?
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums