Results 1 to 9 of 9

Thread: [RESOLVED] [Oracle 9i] PL/SQL Date Arithmetic

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved [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:
    1. /*
    2.                   Anonymous block to get all sundays between a given date range
    3. */
    4. DECLARE
    5. FROM_DT DATE:= TO_DATE('01/01/2009','mm/dd/yyyy');
    6. TO_DT DATE:= TO_DATE('01/31/2009','mm/dd/yyyy');
    7. X VARCHAR2(30);
    8. BEGIN
    9.       WHILE NOT FROM_DT > TO_DT
    10.       LOOP
    11.         SELECT NEXT_DAY(FROM_DT,'SUN') INTO X from dual;
    12.          --I want to print X only if it is a SUNDAY and just once.
    13.                 DBMS_OUTPUT.PUT_LINE(X);
    14.         FROM_DT := FROM_DT + 1;
    15.       END LOOP;  
    16. 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

  2. #2

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [Oracle 9i] PL/SQL Date Arithmetic

    This is version #2. Still no luck.

    sql Code:
    1. /*
    2.               Anonymous block to get all sundays between a given date range
    3. */
    4. DECLARE
    5.   from_dt DATE := to_date('01/01/2009', 'mm/dd/yyyy');
    6.   to_dt   DATE := to_date('01/30/2009', 'mm/dd/yyyy');
    7.   x       VARCHAR2(30);
    8. BEGIN
    9.  
    10.   WHILE NOT FROM_DT > TO_DT
    11.   LOOP
    12.     IF to_date(x, 'dd-mon-yyyy') >= to_dt THEN
    13.       dbms_output.put_line('hello');
    14.       EXIT;
    15.     ELSE
    16.       --dbms_output.put_line('FROM DATE ==>' || from_dt);
    17.       x := next_day(from_dt, 'SUN');
    18.       IF to_date(x, 'DD-MON-YYYY') > to_dt THEN
    19.         dbms_output.put_line('Hello');
    20.       ELSE
    21.         dbms_output.put_line('Sundays -->' || x);
    22.       END IF;
    23.     END IF;
    24.     from_dt := from_dt + 7;
    25.   END LOOP;
    26. 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

  3. #3
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    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

  4. #4

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [Oracle 9i] PL/SQL Date Arithmetic

    Quote Originally Posted by BackWoodsCoder View Post
    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

  5. #5
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    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:
    1. to_dt   DATE := to_date('01/30/2009', 'mm/dd/yyyy');

    vb Code:
    1. IF to_date(x, 'dd-mon-yyyy') >= to_dt THEN

    vb Code:
    1. 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

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  8. #8

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: [Oracle 9i] PL/SQL Date Arithmetic

    Quote Originally Posted by BackWoodsCoder View Post
    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:
    1. to_dt   DATE := to_date('01/30/2009', 'mm/dd/yyyy');

    vb Code:
    1. IF to_date(x, 'dd-mon-yyyy') >= to_dt THEN

    vb Code:
    1. 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

  9. #9
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    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
  •  



Click Here to Expand Forum to Full Width