user1664398
user1664398

Reputation: 113

Oracle SQL for handling null values

I have a table with 3 columns: c1, c2 and c3. I want to write a select statement so that if both c1 and c2 are null, display c3, otherwise display c1.

What would be the best way to write a select statement in the above scenario?

Upvotes: 2

Views: 4071

Answers (1)

Maheswaran Ravisankar
Maheswaran Ravisankar

Reputation: 17920

Using CASE

CASE WHEN C1 IS NULL AND C2 IS NULL
     THEN C3
     ELSE C1
END

Using NVL2

NVL2(C1,C1,NVL2(C2,C1,C3))

OR

NVL2(C1||C2,C1,C3)

Using DECODE

DECODE(C1,NULL,DECODE(C2,NULL,C3,C1),C1)

Readability and ease wise CASE wins.

Upvotes: 8

Related Questions