SELECT TO_DATE('','yyyymmdd');
doesn't evaluates to NULL
since you passing an empty string instead of NULL
as an argument to TO_DATE()
This will successfully evaluate to NULL
SELECT TO_DATE(NULL,'yyyymmdd');
If you expect an empty string and want to treat it as a NULL
you can use NULLIF()
SELECT TO_DATE(NULLIF(dt, ''),'yyyymmdd')
FROM
(
SELECT CAST('' AS VARCHAR(32)) dt
) q
That being said your sample code that evaluates (1) as NULL
SELECT COALESCE(
TO_DATE(NULLIF('', ''),'yyyymmdd'), --(1)
TO_DATE(NULLIF('20130201',''),'yyyymmdd') --(2)
);
and returns
| COALESCE | ----------------------------------- | February, 01 2013 00:00:00+0000 |
Here is SQLFiddle demo