Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Dell PowerEdge Servers
  #1  
Old October 22nd, 2003, 09:49 PM
augustd augustd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 11 augustd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Select all dates between two dates

How can I select all of the dates between two dates?

For example, given the start date 12/2/2003 and the end date 12/5/2003 I want to return:

12/2/2003
12/3/2003
12/4/2003
12/5/2003

Is there a built in function for this? Is there a way for a function to return multiple rows?

It has to be a function because I need to use it within other SQL statements.

Reply With Quote
  #2  
Old October 23rd, 2003, 08:25 AM
cconstantine cconstantine is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 60 cconstantine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 27 sec
Reputation Power: 5
Assuming you have a "DATE" Oracle type column in a table. Let's presuem the column is named "foo". Then you want, in your WHERE cluase:

TO_DATE('12/2/2003','MM/DD/YYYY') <= foo
AND
TO_DATE('12/5/2003','MM/DD/YYYY') >= foo

you may obviously change the literal date strings which are the first args to be whate3ver you're searchign for.

Note that I"m assumin you populated the 'foo' column with statements like:

SET (foo) VALUES ( TO_DATE('12/2/2003','MM/DD/YYYY') )

The TO_DATE() above in the WHERE will be midnight on that date... if your 'foo' col has time information (non-midnight date stamp) your where might get a little funny...

-c

Reply With Quote
  #3  
Old October 23rd, 2003, 08:42 AM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
Functions can be return only one value in any programming language. Why dont you use the GROUP BY clause in SQL statement. Can you please send your query that you want to write?. An example of GROUP BY is look like that:

SELECT hiredate from emp
WHERE hiredate between '01-JAN-2003' AND '01-OCT-2003'
GROUP BY hiredate
/

Regards,

Reply With Quote
  #4  
Old October 23rd, 2003, 12:10 PM
augustd augustd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 11 augustd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
GROUP BY won't work for this project because it won't return any dates for which there are no entries in the table. Lets say we have a (simplified) table of orders like this:

order_id INTEGER
order_date DATE
order_total NUMBER(10,2)

If we have these entries:

1 12/2/2003 12.30
2 12/2/2003 45.60
3 12/4/2003 78.90
4 12/5/2003 10.23

I need to select all the dates and how much was sold on each one, even 12/3 when nothing was sold:

date num amount
12/2/2003 2 57.90
12/3/2003 0 00.00
12/4/2003 1 78.90
12/5/2003 1 10.23

So, how do I fill in the extra dates with no entries? My initial thought was to somehow select each day between the two endpoints, outer join them to the orders, then group by to get the totals...

It has to be SQL, I can't use a procedure in this context.

Reply With Quote
  #5  
Old October 23rd, 2003, 01:21 PM
Dan Drillich Dan Drillich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 Dan Drillich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Hi augustd,

You asked: So, how do I fill in the extra dates with no entries?

One way to do it is via an inline-view, which will hold all the dates in the range.
For example, the following query produces all the dates in the month of August (-2: two months ago).
Then, you can use an outer join with this inline-view to ensure that each day is in the result set.


select to_char(sysdate + 1 - rownum,'DD-Mon') as d
from all_objects
where trunc(sysdate + 1 - rownum,'MM') >= trunc(add_months(sysdate,-2),'MM')
minus
select to_char(sysdate + 1 - rownum,'DD-Mon') as d
from all_objects
where trunc(sysdate + 1 - rownum,'MM') > trunc(add_months(sysdate,-2),'MM')

Cheers,
Dan

Reply With Quote
  #6  
Old October 23rd, 2003, 02:27 PM
augustd augustd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 11 augustd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Arrow

We have a winner! Thanks Dan.

I modified it a little bit to get my arbitrary date range:

SELECT TO_DATE('12/01/2003', 'MM/DD/YYYY') - 1 + rownum AS d
FROM all_objects
WHERE TO_DATE('12/01/2003', 'MM/DD/YYYY') - 1 + rownum <= TO_DATE('12/05/2003', 'MM/DD/YYYY')

Returns:

D
2003-12-01 00:00:00
2003-12-02 00:00:00
2003-12-03 00:00:00
2003-12-04 00:00:00
2003-12-05 00:00:00

Reply With Quote
  #7  
Old August 20th, 2004, 03:12 AM
sunil2205 sunil2205 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 sunil2205 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Details abt rownum

Hi can tell me what rownum implies in ur query???

Reply With Quote
  #8  
Old August 20th, 2004, 08:00 AM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
In Oracle rownum is a pseudo column and its value not saved in the database, it shows the number of each record in the table, for example if you have five records in your table called 'department' and you run the query using rownum defined in your SELECT clause then your output would be look like that:


SELECT rownum, deptno, dname
FROM department
/

rownum deptno dname
------- ------- -------------------
1 10 ACCOUNTS
2 20 SALES
3 30 MARKETING
4 40 PURCHASE
5 50 I.T

Reply With Quote
  #9  
Old August 20th, 2004, 01:45 PM
augustd augustd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 11 augustd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Right, so date + rownum has the effect of adding one day to each date returned.

So to return the next ten days:

select rownum, trunc(sysdate + rownum) AS day
from all_objects
where rownum <= 10

ROWNUM DAY
1 2004-08-21 00:00:00
2 2004-08-22 00:00:00
3 2004-08-23 00:00:00
4 2004-08-24 00:00:00
5 2004-08-25 00:00:00
6 2004-08-26 00:00:00
7 2004-08-27 00:00:00
8 2004-08-28 00:00:00
9 2004-08-29 00:00:00
10 2004-08-30 00:00:00

Reply With Quote
  #10  
Old August 31st, 2004, 03:42 AM
raza ul haq raza ul haq is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 5 raza ul haq User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
problem while inserting values

dear sir
i have a button on a form.
behind that button i have
one select statement and othe insert into statement after one an other

if i run only insert statement it works
but
if i run select statement first and after that an insert statement ,
it gives error
cannot insert.
please help me for that problem

raza

Reply With Quote
  #11  
Old August 31st, 2004, 12:04 PM
augustd augustd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 11 augustd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by raza ul haq
dear sir
i have a button on a form. behind that button i have
one select statement and othe insert into statement after one an other

if i run only insert statement it works
but if i run select statement first and after that an insert statement, it gives error cannot insert.
please help me for that problem

raza


I'm not sure what you are asking. Are you getting an error message? What is your SQL? Does this have to do with dates?

Reply With Quote
  #12  
Old August 31st, 2004, 01:53 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
By mistaken someone posted his question here, it is a separate thread, this question does not concern with date.

Reply With Quote
  #13  
Old September 1st, 2004, 09:40 AM
gcisjxs0 gcisjxs0 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 8 gcisjxs0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
FYI - Dan's solution will work great. Not that you should every run into this but just so your informed. You will only be able to return the number of records that exist in the table in your from clause. Also keep in mind that the table you use can effect performance.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Select all dates between two dates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |