I have something similar to below in my SQL that I am looking to simplify the CASE WHEN statement
I have multiple variables (i.e. CODE1, CODE2, all the way through CODE10 that I want to run through a CASE WHEN any of those codes are found in my "CODES" table to create a "CODE\_FLAG" variable.

TABLE2 would contain a list of codes (that might be updated some day).
So end result would be Selecting all records from Table1 as well as a created variable called CODE\_FLAG that is simply a 'Y' or 'N' based on the list found in TABLE2. TABLE1 has up to 10 Codes listed as CODE1, CODE2, CODE3...etc.

**TABLE2**

| CODES |
| --- |
| 98761234 |
| 43216789 |
| 11111111 |

Here's what I have so far. Is there a way to

```
WITH T2,
(Select CODES
FROM TABLE2)

SELECT  T1.*,
CASE WHEN T1.CODE1, T1.CODE2, T1.CODE3, T1.CODE4 IN (T2.CODES) 
THEN 'Y', ELSE 'N' END AS CODE_FLAG
FROM TABLE1 T1
CROSS JOIN T2 T2

```

or maybe something like this?

```
WITH T2,
(Select CODES
FROM TABLE2)

SELECT  T1.*,
CASE WHEN T1.CODE1 or T1.CODE2 or T1.CODE3 or T1.CODE4 IN (select T2.CODES from T2 T2) 
THEN 'Y', ELSE 'N' END AS CODE_FLAG
FROM TABLE1 T1

```

I currently have a much longer version

```
WITH T2,
(Select CODES
FROM TABLE2)

SELECT  T1.*,
CASE WHEN T1.CODE1 in (select CODES from T2) 
or T1.CODE2 in (select CODES from T2)
or T1.CODE3 in (select CODES from T2)
or T1.CODE4 in (select CODES from T2) 
THEN 'Y', ELSE 'N' END AS CODE_FLAG
FROM TABLE1 T1

```