Home > Net >  Oracle sql case when exclude cells containg string
Oracle sql case when exclude cells containg string

Time:02-01

I have this sql query which works well:

SELECT distinct
A,
CASE
WHEN Parameter = 'ID' and regexp_like (VALUE, 'FOO')
 THEN VALUE
  ELSE 'NA'
 END AS test FROM my_table;

my_table

A parameter value
x ID FOO1223
y ID FOO5462
z ID empy
p ID BAR5432

result:

A value
x FOO1223
y FOO5462
z NA
p NA

Now I would like to exclude VALUE that starts with 'BAR'. How can I add this to the CASE...WHEN statement?

The output should look this:

A value
x FOO1223
y FOO5462
z NA

CodePudding user response:

Do NOT LIKE in the WHERE clause to skip the rows where value start with BAR.

SELECT distinct
A,
CASE
WHEN Parameter = 'ID' and regexp_like (VALUE, 'FOO')
 THEN VALUE
  ELSE 'NA'
 END AS test FROM my_table
WHERE value NOT LIKE 'BAR%' 
  • Related