0

I am using Oracle and am trying to build out some sql for the following scenario:

On EMPLOYEES table, if employee has ADDRESS3 not equal to ' ', populate this field with ADDRESS2 else, populate this field with ADDRESS1.

...

, ADDRESS_LINE2 = NVL(( 
    SELECT (CASE t2.ADDRESS3 != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END)
    FROM EMPLOYEES t2
    WHERE t2.EMPLID = PS_Z_EXS_EMP_TBL_T.EMPLID
), t2.ADDRESS1)

...

but it keeps giving me an error message about missing the right parenthesis. When I comment this bit out though it runs fine. DOes anyone know what I'm doing wrong?

2
  • 4
    This appears to be missing the WHEN statement. CASE WHEN t2.ADDRESS3 != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END or CASE t2.ADDRESS3 WHEN != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END Commented Aug 13, 2014 at 19:30
  • This appears to be the answer :) Thanks Commented Aug 13, 2014 at 19:37

2 Answers 2

1

CASE has two variants - both needs WHEN clauses.

One variant can have complete and complex boolean expression in each WHEN clause:

CASE
   WHEN t2.ADDRESS3 != ' ' THEN t2.ADDRESS2
   ELSE t2.ADDRESS1
END

In the other variant each WHEN clause contain values to be tested for the CASE expression:

CASE t2.ADDRESS3
   WHEN ' ' THEN t2.ADDRESS1
   ELSE t2.ADDRESS2
END

The last one cannot do != so therefore "reversed" logic ;-)

Sign up to request clarification or add additional context in comments.

Comments

0

You need the END keyword at the end of a case expression:

CASE t2.ADDRESS3 != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END

2 Comments

Then check for a mis-placed closing parentheses.
WHEN was also missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.