Home » SQL & PL/SQL » SQL & PL/SQL » using date in a for... loop
using date in a for... loop [message #179747] Wed, 28 June 2006 09:57 Go to next message
pully
Messages: 3
Registered: October 2005
Junior Member
How do i write this loop

i have this three text items as dates:
:date_from
:date_to
:required_days

and my loop is as follows:

:date_to := :date_from + :required_days
for i in :date_from..:date_to loop
if to_char(i,'DAY') = 'SATURDAY' AND to_char(i+1,'DAY') ='SUNDAY' then :date_to := :date_to + 1;
end loop;
Re: using date in a for... loop [message #179752 is a reply to message #179747] Wed, 28 June 2006 10:05 Go to previous messageGo to next message
JSI2001
Messages: 1016
Registered: March 2005
Location: Scotland
Senior Member
Quote:

i have this three text items as dates:

well firt off, don't have text items as dates, have date items as dates. dont have text items for nubers either, use number items.

You also have:
if to_char(i,'DAY') = 'SATURDAY' AND to_char(i+1,'DAY') ='SUNDAY' then :date_to := :date_to + 1;

How can the 'date' be both saturday AND sunday (No comment about timezones guys Smile )

a for loop, loops through numbers not strings.

what exactly is it that you are trying to do?
Re: using date in a for... loop [message #179778 is a reply to message #179752] Wed, 28 June 2006 12:37 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
I wholeheartedly agree with Jim. Jim though may have missed the illogical logic that OP was comparing i and i+1.

OP, you can use your code after you use dates for dates, but you would want to loop from 1 to the difference between the two dates and then add i to the start date (use a date and not a character string!) instead of thinking that you can loop between dates (or in your case character strings).

[Updated on: Wed, 28 June 2006 12:37]

Report message to a moderator

Re: using date in a for... loop [message #179802 is a reply to message #179778] Wed, 28 June 2006 16:13 Go to previous message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
If you, however, want to loop between dates, that can be done too: convert it into the Julian date (which is, actually, a number) and that should do it. For example:
BEGIN
  FOR i IN TO_NUMBER(TO_CHAR(TO_DATE('20.06.2006.', 'dd.mm.yyyy.'), 'j')) ..
           TO_NUMBER(TO_CHAR(TO_DATE('25.06.2006.', 'dd.mm.yyyy.'), 'j'))
  LOOP
     dbms_output.put_line(TO_DATE(i, 'j'));
  END LOOP;
END;
/
Previous Topic: DOT NOTATION -- DB LINKS
Next Topic: Help in WHERE condition in SELECT statement
Goto Forum:
  


Current Time: Thu Sep 04 12:48:02 CDT 2025