Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQL LIKE condition with multiple values in PostgreSQL?

Is there any shorter way to look for multiple matches:

 SELECT *   from table   WHERE column LIKE "AAA%"      OR column LIKE "BBB%"      OR column LIKE "CCC%" 

This questions applies to PostgreSQL 9.1, but if there is a generic solution it would be even better.

like image 349
sorin Avatar asked Oct 18 '12 15:10

sorin


People also ask

Can we use multiple values in like operator in SQL?

“The SQL LIKE operator allows performing logical evaluation for any matching records. Using the LIKE operator, you can specify single or multiple conditions. This allows you to perform an action such as select, delete, and updating any columns or records that match the specified conditions.

How use like statement in multiple values in SQL?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.

How do you do multiple conditions in like?

select * from employee where name like '%aa%' or name like '%bb% or name like '%cc%'..... Next row NAME Column Contains aa bb cc dd..... each value in the NAME column and put in the multiple like operator using OR condition.

How do I test multiple values in PostgreSQL?

PostgreSQL IN operator syntax You use IN operator in the WHERE clause to check if a value matches any value in a list of values. The syntax of the IN operator is as follows: value IN (value1,value2,...) The IN operator returns true if the value matches any value in the list i.e., value1 , value2 , …


1 Answers

Perhaps using SIMILAR TO would work ?

SELECT * from table WHERE column SIMILAR TO '(AAA|BBB|CCC)%'; 
like image 191
tozka Avatar answered Sep 21 '22 13:09

tozka