|
-
Nov 3rd, 2009, 03:24 PM
#1
[RESOLVED] [Oracle 9i] PL/SQL Date Arithmetic
I have this block which gives me all the SUNDAYS between a given date range. This prints all the sundays, but there's a bug in this code.
sql Code:
/*
Anonymous block to get all sundays between a given date range
*/
DECLARE
FROM_DT DATE:= TO_DATE('01/01/2009','mm/dd/yyyy');
TO_DT DATE:= TO_DATE('01/31/2009','mm/dd/yyyy');
X VARCHAR2(30);
BEGIN
WHILE NOT FROM_DT > TO_DT
LOOP
SELECT NEXT_DAY(FROM_DT,'SUN') INTO X from dual;
--I want to print X only if it is a SUNDAY and just once.
DBMS_OUTPUT.PUT_LINE(X);
FROM_DT := FROM_DT + 1;
END LOOP;
END;
In the above code, I want to print each date, just once.
Any idea?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Nov 3rd, 2009, 03:58 PM
#2
Re: [Oracle 9i] PL/SQL Date Arithmetic
This is version #2. Still no luck.
sql Code:
/*
Anonymous block to get all sundays between a given date range
*/
DECLARE
from_dt DATE := to_date('01/01/2009', 'mm/dd/yyyy');
to_dt DATE := to_date('01/30/2009', 'mm/dd/yyyy');
x VARCHAR2(30);
BEGIN
WHILE NOT FROM_DT > TO_DT
LOOP
IF to_date(x, 'dd-mon-yyyy') >= to_dt THEN
dbms_output.put_line('hello');
EXIT;
ELSE
--dbms_output.put_line('FROM DATE ==>' || from_dt);
x := next_day(from_dt, 'SUN');
IF to_date(x, 'DD-MON-YYYY') > to_dt THEN
dbms_output.put_line('Hello');
ELSE
dbms_output.put_line('Sundays -->' || x);
END IF;
END IF;
from_dt := from_dt + 7;
END LOOP;
END;
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Nov 3rd, 2009, 04:06 PM
#3
Fanatic Member
Re: [Oracle 9i] PL/SQL Date Arithmetic
so what's the output like? You say you want to print the dates just once, I'm assuming you're repeating sunday's somewhere in your result set?
Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".
VS 2008 .NetFW 2.0
-
Nov 3rd, 2009, 04:58 PM
#4
Re: [Oracle 9i] PL/SQL Date Arithmetic
 Originally Posted by BackWoodsCoder
so what's the output like? You say you want to print the dates just once, I'm assuming you're repeating sunday's somewhere in your result set?
Here's my output.
Output:
Sundays -->04-JAN-09
Sundays -->11-JAN-09
Sundays -->18-JAN-09
Sundays -->25-JAN-09
Sundays -->01-FEB-09 <-- Should not print 01-FEB-2009
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Nov 3rd, 2009, 05:04 PM
#5
Fanatic Member
Re: [Oracle 9i] PL/SQL Date Arithmetic
here's a stupid question, why do you format your dates one way in the beginning but use a different format later on in your code?
vb Code:
to_dt DATE := to_date('01/30/2009', 'mm/dd/yyyy');
vb Code:
IF to_date(x, 'dd-mon-yyyy') >= to_dt THEN
vb Code:
IF to_date(x, 'DD-MON-YYYY') > to_dt THEN
Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".
VS 2008 .NetFW 2.0
-
Nov 3rd, 2009, 06:07 PM
#6
Re: [Oracle 9i] PL/SQL Date Arithmetic
If x<= to_dt then
dbms_output.put_line('Sundays -->' || x);
End If;
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Nov 3rd, 2009, 09:20 PM
#7
Re: [Oracle 9i] PL/SQL Date Arithmetic
Inclusion of extraneous date due to this line x := next_day(from_dt, 'SUN'); which made you jump to next week without validation (validation was performed in WHILE condition).
Use this instead... more reusable.
Code:
CREATE OR REPLACE TYPE DATE_COLLTYP
AS TABLE OF DATE;
/
GRANT EXECUTE ON DATE_COLLTYP TO PUBLIC; -- grant to public since its just an "array" data type
/
CREATE OR REPLACE FUNCTION list_days (start_dt DATE, end_dt DATE, weekday CHAR)
RETURN date_colltyp
AS
ret_list date_colltyp := date_colltyp(); -- initialize collection
v_date DATE;
BEGIN
BEGIN
v_date := NEXT_DAY(start_dt, weekday); -- first sunday after start date
WHILE v_date <= end_dt
LOOP
ret_list.EXTEND;
ret_list(ret_list.COUNT) := v_date;
v_date := v_date + 7;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
NULL; -- or log error via autonomous transaction, or raise_application_error
END;
RETURN ret_list;
END;
/
/* sample usage via SELECT */
SELECT TO_CHAR(COLUMN_VALUE, 'YYYY-MM-DD') AS sundays
FROM TABLE(list_days(SYSDATE - 100, SYSDATE, 'SUN')); -- use table function to convert collection to result set
SELECT *
FROM TABLE(list_days(TRUNC(SYSDATE - 100), TRUNC(SYSDATE), 'mondays'));
And I suggest you also create collection types for NUMBER, VARCHAR2(4000), and VARCHAR2(32767). varchar2_32k_colltyp can be used for functions that wrap file I/O using UTL_FILE. varchar2_4k can be used for functions that wrap java.io.File, e.g. directory listing via Java stored procedure.
Last edited by leinad31; Nov 3rd, 2009 at 09:29 PM.
-
Nov 4th, 2009, 08:34 AM
#8
Re: [Oracle 9i] PL/SQL Date Arithmetic
 Originally Posted by BackWoodsCoder
here's a stupid question, why do you format your dates one way in the beginning but use a different format later on in your code?
vb Code:
to_dt DATE := to_date('01/30/2009', 'mm/dd/yyyy');
vb Code:
IF to_date(x, 'dd-mon-yyyy') >= to_dt THEN
vb Code:
IF to_date(x, 'DD-MON-YYYY') > to_dt THEN
X is a varchar and the Next_Day function returns the date in that particular format. So the conversion.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Nov 4th, 2009, 09:48 AM
#9
Fanatic Member
Re: [RESOLVED] [Oracle 9i] PL/SQL Date Arithmetic
Ah, I see, couldn't wrap my head around that one.
Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".
VS 2008 .NetFW 2.0
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|