I am trying to use COALESCE in WHERE clause, and I am getting the following error:

ORA-00907: missing right parenthesis Failed SQL stmt:

If I remove the COALESCE, I don't get the error anymore. I am not sure why would it give me this error as the parenthesis seem correct. Here's my SQL statement:

SELECT S.OPRID, A.OPRNAME, S.EMAIL_ADDR
FROM TABLE1 S, TABLE2 A
WHERE COALESCE(S.REHIRE_DT,S.ORIG_HIRE_DT)
        BETWEEN (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM- DD'),'YYYY-MM-DD') - 3 DAY)
            AND (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD') - 1 DAY)
AND S.EMPLSTATUS = 'A'
AND A.EMPLID     = S.EMPLID
ORDER BY S.OPRID
有帮助吗?

解决方案

Take the "DAY" word out. That is not used in oracle in that manner:

SELECT S.OPRID, A.OPRNAME, S.EMAIL_ADDR
FROM TABLE1 S, TABLE2 A
WHERE COALESCE(S.REHIRE_DT,S.ORIG_HIRE_DT)
        BETWEEN (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM- DD'),'YYYY-MM-DD') - 3 )
            AND (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD') - 1 )
AND S.EMPLSTATUS = 'A'
AND A.EMPLID     = S.EMPLID
ORDER BY S.OPRID

"DAY" is a keyword to be used as part of the EXTRACT function - see Here The default unit of subtraction from a date field is already in units of days. I am not familiar with DB2, but I am assuming that your usage of DAY is a DB2-specific attribute. That is probably not portable SQL. Yes, oracle's error messages can be confusing at times.

Now that I think about it, you can replace all that "TO_DATE" stuff with just:

BETWEEN TRUNC(SYSDATE)-3 AND TRUNC(SYSDATE)-1
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top