Really Simple Date conversion function... but doesn't work

For my application I need a function that converts a date to a date that is a monday.
I wrote this simple function, and I've been trying for many many times... but it just doesn't work. It returns a null value. The problem lies with the IF-statement.
I'm using iSQL 9
CREATE OR REPLACE FUNCTION to_monday ( datum IN DATE )
  RETURN DATE
  AS
BEGIN
  FOR n IN 0..6 LOOP
    IF ( TO_CHAR(datum + n, 'day') = 'friday' ) THEN
      RETURN (datum + n - 4);
    END IF;
  END LOOP;
END;
SELECT to_monday(CURRENT_DATE) FROM dual;Edited by: popovitsj on 11-mrt-2012 5:07
Edited by: popovitsj on 11-mrt-2012 5:09
Edited by: popovitsj on 11-mrt-2012 5:09
Edited by: popovitsj on 11-mrt-2012 5:12

popovitsj wrote:
For my application I need a function that converts a date to a date that is a monday.
I wrote this simple function, and I've been trying for many many times... but it just doesn't work. It returns a null value. The problem lies with the IF-statement.
I'm using iSQL 9
CREATE OR REPLACE FUNCTION to_monday ( datum IN DATE )
RETURN DATE
AS
BEGIN
FOR n IN 0..6 LOOP
IF ( TO_CHAR(datum + n, 'day') = 'friday' ) THEN
RETURN (datum + n - 4);
END IF;
END LOOP;
END;
SELECT to_monday(CURRENT_DATE) FROM dual;Edited by: popovitsj on 11-mrt-2012 5:07
Edited by: popovitsj on 11-mrt-2012 5:09
Edited by: popovitsj on 11-mrt-2012 5:09
Edited by: popovitsj on 11-mrt-2012 5:12You have to use format modifier. to_char(...,'day') will return "friday" with padded space. You have to use to_char(..,'fmday').
SQL> CREATE OR REPLACE FUNCTION to_monday ( datum IN DATE )
  2    RETURN DATE
  3    AS
  4  BEGIN
  5    FOR n IN 0..6 LOOP
  6      IF ( TO_CHAR(datum + n, 'fmday') = 'friday' ) THEN
  7        RETURN (datum + n - 4);
  8      END IF;
  9    END LOOP;
10  END;
11  /
Function created.
SQL> SELECT to_monday(sysdate) from dual;
TO_MONDAY
12-MAR-12

Similar Messages

Maybe you are looking for

  • "Look Up" Question

    I need help on how to look up a multi column MULTI ROW table given two variables which exist row one and column one cells. Something like .... Lookup Column One to find X then lookup Row One to find Y then pull the content of cell XY and insert ... J

  • Cannot open itunes at all. Please help

    I have recently upgraded to the newest version of itunes but everytime I try to open the program it says that a newer version of quicktime is needed to open itunes and for me to reinstall itunes. Well I have done this about twenty times and it still

  • How to output column of text to array

    Hi, In attached vi,  How can I output all column of text into 1D array? I only able to read the first line. Thanks. Attachments: 1.vi ‏8 KB

  • How to best sync multiple collections of contacts between multiple users

    What is the best way to sync multiple collections of contacts among multiple users on iOS and OS X? For example, suppose I have three collections of contacts: Bob's, Sally's, and Common. I want Bob to be able to access and sync Bob's contacts and Com

  • WHERE DID THE POWER GO?!?

    I woke up this morning and couldn't access my Airport... so I came upstairs to check it and it wasn't on. So I checked everything else plugged into that surge protector was on and running fine, including the modem that I'm currently plugged into. I u