Is there a function in postgres like contains
? that can be used in the where clause
to check , whether the string passed is contained in column?
Postgresql Contains in where clause
Asked Answered
There are a bunch of ways of solving this:
Use
like
,ilike
, and/orSIMILAR TO
along with ||. To handle columns, something like:WHERE col1 ilike '%' || col2 || '%';
Use position as NPE's answer
You could also use
regexp_matches
but that is more complex.
It is possible to use
col1 ~ 'abc'
or col1 ~ col2
also for this case. –
Lesialesion I find that the performance of ~ is much better than LIKE (YMMV) –
Llywellyn
~
is actually a REGEXP. The alias of like is ~~
–
Dynamite You could use position()
for that. It returns zero if the substring is not found:
position(col2 in col1) <> 0
There are a bunch of ways of solving this:
Use
like
,ilike
, and/orSIMILAR TO
along with ||. To handle columns, something like:WHERE col1 ilike '%' || col2 || '%';
Use position as NPE's answer
You could also use
regexp_matches
but that is more complex.
It is possible to use
col1 ~ 'abc'
or col1 ~ col2
also for this case. –
Lesialesion I find that the performance of ~ is much better than LIKE (YMMV) –
Llywellyn
~
is actually a REGEXP. The alias of like is ~~
–
Dynamite You can use:
select * from TABLE_NAME where TABLE_NAME like '%a%';
%a% > the results going to show you all the data that contains an a in any position of the value.
also, you can use:
%a > values where ending an a
a% > values starting with a
_a% > values with a in the second position
© 2022 - 2024 — McMap. All rights reserved.
LIKE '%abc%'
in SQL Server? – Diversiform