Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue when comparing result of to_char(myDate, 'DAY') to a string

I have been trying to find what the issue might be but I am just out of luck and don't understand this problem at all.I have the following code:

CREATE OR REPLACE FUNCTION ckeckDay(dateC in date)
RETURN VARCHAR
IS
  day VARCHAR(15);
  checkFriday VARCHAR(1);
BEGIN
  checkFriday := 'N';
  day := to_char(dateC, 'DAY');
  IF day = 'FRIDAY' THEN
    checkFriday := 'Y';
  END IF;
  RETURN day;
END;
/

the dateC is set to Friday (even tested it by returning day instead of the day variable and it returns Friday.) However the IF statement never evaluates to true even though the day variable is indeed Friday.Any ideas how to go around this issue.Thanks

like image 344
Tohmas Avatar asked Nov 07 '12 12:11

Tohmas


2 Answers

If you want to be really robust about this then you ought to force the NLS setting to English and apply the "fill mode" format model FM for trimming leading and trailing spaces.

If To_Char(DateC,'fmDAY', 'nls_date_language=english') = 'FRIDAY'
Then ...
like image 182
David Aldridge Avatar answered Oct 25 '22 10:10

David Aldridge


It is because day variable contains a blank padded value. Use trim function to get rid of leading and trailing spaces:

IF trim(day) = 'FRIDAY' THEN
  checkFriday := 'Y';
END IF;

And please use VARCHAR2 datatype for string variables. Do not use VARCHAR.

like image 24
Nick Krasnov Avatar answered Oct 25 '22 11:10

Nick Krasnov