Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decode two values using oracle decode

Tags:

oracle

select status_a,status_b from test 

how to decode status_a,status_b below values using oracle decode function aand if one of the values of status_a or status_b is null.

if  status_a='Y' and status_b='Y' then 'Y'

if  status_a='Y' and status_b='N' then 'N'

if  status_a='N' and status_b='Y' then 'N'

if  status_a='N' and status_b='N' then 'N'

Regards,

Chaituhara

like image 400
user2750658 Avatar asked Jan 12 '23 10:01

user2750658


1 Answers

Why do you want to use DECODE? CASE would seem like a much better fit

CASE WHEN status_a = 'Y' and status_b = 'Y' THEN 'Y'
     WHEN status_a = 'Y' and status_b = 'N' THEN 'N'
     WHEN status_a = 'N' and status_b = 'Y' THEN 'N'
     WHEN status_a = 'N' and status_b = 'N' THEN 'N'
  END

Of course, the logic you posted does not appear to make sense. The only way that status_a = 'Y' or status_b = 'Y' would evaluate to FALSE while status_a = 'Y' or status_b = 'N' evaluated to TRUE would be if status_a = 'N' and status_b = 'N'. But that means that the third and fourth branch would never be reached. If you meant and rather than or, the logic would make sense. But in that case, you could simplify it to

CASE WHEN status_a = 'Y' and status_b = 'Y' THEN 'Y'
     ELSE 'N'
  END
like image 80
Justin Cave Avatar answered Jan 22 '23 15:01

Justin Cave