1

i have an array of strings and a column which may contain one or more of those strings(seperated by space) i want to get all rows where this column contains one of the strings. Since the values all have 3 letters and therefore can't contain each other, i know i could just write

SELECT * FROM table WHERE 
column LIKE '%val1%' OR 
column LIKE '%val2%' OR 
column LIKE '%val3%' OR 
column LIKE '%val4%'

But i'm wondering if there isn't an easier statement, like column IN ('val1', 'val2', 'val3', 'val4') (This one seems only to work when the entry is equal to one of the values, but not if it just contains them)

Ginso89
  • 139
  • 1
  • 4
  • 9
  • Does this answer your question? [SQL Multiple LIKE Statements](https://stackoverflow.com/questions/30975811/sql-multiple-like-statements) – Refilon Dec 11 '19 at 08:29
  • 2
    Which [DBMS](https://en.wikipedia.org/wiki/DBMS) product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a [tag](https://stackoverflow.com/help/tagging) for the database product you are using. [Why should I tag my DBMS](https://meta.stackoverflow.com/questions/388759/) –  Dec 11 '19 at 08:32
  • 1
    "*seperated by space*" might indicate a wrong database design and those strings should actually be converted to rows in a second table with a one-to-many relationship –  Dec 11 '19 at 08:34

2 Answers2

1

Try reading this Is there a combination of "LIKE" and "IN" in SQL? and Combining "LIKE" and "IN" for SQL Server , this will solve you question.

Something like this from the first link.

SQL Server:

WHERE CONTAINS(t.something, '"bla*" OR "foo*" OR "batz*"')
Buchiman
  • 320
  • 5
  • 18
0

Ist oracle you could use regular expressions

select  * 
from    table 
where   regexp_like (column,'val(1|2|3|4)')
Thomas Strub
  • 1,275
  • 7
  • 20