Name of day padded with spaces prevent proper behavior

42 views Asked by At

I'm trying to go in a if condition in PLSQL when certaint dates are on a specific day in the week.

DECLARE
  v_gebDatum    CONSTANT     DATE                 := to_date('21-01-1995', 'dd-MM-yyyy');
  v_mathDatum                DATE                 := v_gebDatum;
  v_huidigeDag               VARCHAR2(20);

  v_teller                   number   := 0;
BEGIN
  WHILE to_char(v_mathDatum, 'yyyy') < to_char(sysdate, 'yyyy')
  LOOP
    v_mathDatum := add_months(v_mathDatum, 12);
    v_huidigeDag := to_char(v_mathDatum, 'DAY');
    dbms_output.put_line(v_huidigeDag);
    IF v_huidigeDag IN('ZATERDAG', 'ZONDAG')
    THEN
      dbms_output.put_line(v_mathDatum);
    END IF;
  END LOOP;
END;

Problem is that I can't get this to work. When printing the v_huidigeDag it clearly has the values 'ZATERDAG and 'ZONDAG' in some of the printed lines.

It appears that, for some reason when I printed the values for daynames the program added spaces in. I guess it takes the char length for the longest day name ('DONDERSDAG') in my Locale. And everything with less characters than that it adds spaces. So zaterdag becomes = 'ZATERDAG ' and zondag becomes = 'ZONDAG '. Now it's working but does somebody know an easy way to prevent this?

1

There are 1 answers

0
Sylvain Leroux On BEST ANSWER

The problem is with the DAY format. According to the date format documentation:

DAY

Name of day, padded with blanks to display width of the widest name of day in the date language used for this element.

To disable that behavior, you have to use the FM modifier:

FM

In a datetime format element of a TO_CHAR function, this modifier suppresses blanks in subsequent character elements (such as MONTH) and suppresses leading zeroes for subsequent number elements (such as MI) in a date format model. Without FM, the result of a character element is always right padded with blanks to a fixed length, and leading zeroes are always returned for a number element. With FM, which suppresses blank padding, the length of the return value may vary.

In your particular case:

    v_huidigeDag := to_char(v_mathDatum, 'fmDAY');