|
Replies:
36
-
Pages:
3
[
1
2
3
| Next
]
-
Last Post:
Oct 8, 2008 9:20 AM
Last Post By: Stewart W. Bryson
|
|
|
Posts:
90
Registered:
05/20/07
|
|
|
|
Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 6:58 AM
|
|
|
|
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 .
|
|
|
Posts:
2,132
Registered:
12/30/05
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:12 AM
in response to: user575545
|
|
|
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
|
|
|
Posts:
566
Registered:
02/14/07
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:13 AM
in response to: user575545
|
|
|
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
|
|
|
Posts:
1,252
Registered:
12/28/05
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:17 AM
in response to: user575545
|
|
|
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.
|
|
|
Posts:
90
Registered:
05/20/07
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:18 AM
in response to: user575545
|
|
|
|
thanks for replies. Just wondering I've never heard of all_objects ,what does it for?
|
|
|
Posts:
1,252
Registered:
12/28/05
|
|
|
Posts:
566
Registered:
02/14/07
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:21 AM
in response to: user575545
|
|
|
|
|
|
Posts:
2,132
Registered:
12/30/05
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:26 AM
in response to: user575545
|
|
|
|
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
|
|
|
Posts:
90
Registered:
05/20/07
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:32 AM
in response to: user575545
|
|
|
|
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
|
|
|
Posts:
2,132
Registered:
12/30/05
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:43 AM
in response to: user575545
|
|
|
|
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.
|
|
|
Posts:
90
Registered:
05/20/07
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:50 AM
in response to: Keith Jamieson
|
|
|
|
so all_objects hold rownum as an object or number of days ?
|
|
|
Posts:
3,496
Registered:
04/26/07
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:54 AM
in response to: user575545
|
|
|
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
|
|
|
Posts:
7,653
Registered:
03/27/08
|
|
|
Posts:
2,132
Registered:
12/30/05
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 7:58 AM
in response to: user575545
|
|
|
|
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
|
|
|
Posts:
90
Registered:
05/20/07
|
|
|
|
Re: Creating Dates without CONNECT BY
Posted:
Jun 25, 2008 8:01 AM
in response to: Keith Jamieson
|
|
|
|
I completely understand what rownum does, my question is why all_objects ,why not dual ?
|
|
|
|
Legend
|
|
Guru : 2500
- 1000000
pts
|
|
Expert : 1000
- 2499
pts
|
|
Pro : 500
- 999
pts
|
|
Journeyman : 200
- 499
pts
|
|
Newbie : 0
- 199
pts
|
|
Oracle ACE Director
|
|
Oracle ACE Member
|
|
Oracle Employee ACE
|
|
Helpful Answer
(5 pts)
|
|
Correct Answer
(10 pts)
|
|