Google Search

Wednesday, July 27, 2011

TO_CHAR Issues

We use TO_CHAR to get month from any date. When we use this to compare, we need to be careful.Did you know TO_CHAR appends white spaces at the end till it is 9 characters long. (Only when used with DATE)

Please see the below demo:

DECLARE
VMON VARCHAR2(20);
BEGIN
select UPPER(to_char(sysdate,'month')) INTO VMON from dual;
DBMS_OUTPUT.PUT_LINE(VMON);
IF VMON = 'JULY' THEN
dbms_output.put_line('Equal');
else
dbms_output.put_line('Not Equal but WHY???');
end if;
IF trim(VMON) = 'JULY' THEN
dbms_output.put_line('Its Equal after TRIM');
else
dbms_output.put_line('Not Equal');
end if;
END;

No comments:

Post a Comment