Skip to main content
1 of 3
Rich
  • 23
  • 4

How to simplify repeating variables in where clause for multiple columns SQL Server

I know there has to be a better way to evaluate a set of repeating variables against multiple columns. I've been searching for a while and still don't have a great solution. Is there a better way to do this?

SELECT *

FROM TABLE

WHERE

  (SUBSTRING(COLUMN1,1,3) IN ('val1','val2','val3','val4'...REPEATS to 30)  
OR SUBSTRING(COLUMN2,1,3) IN ('val1','val2','val3','val4'...REPEATS to 30)  
OR SUBSTRING(COLUMN3,1,3) IN ('val1','val2','val3','val4'...REPEATS to 30)  
OR SUBSTRING(COLUMN4,1,3) IN ('val1','val2','val3','val4'...REPEATS to 30)  
OR SUBSTRING(COLUMN5,1,3) IN ('val1','val2','val3','val4'...REPEATS to 30))   

REPEATS 5 MORE TIMES

Rich
  • 23
  • 4