Search code examples
postgresqlwildcardsql-like

PostgreSQL - How to use wildcard in string condition


I have a string: A%A

I want to find all string same start with A%A in database.

Example:

AABCD - false
AABCE - false
AA%BC - true

I use the sql statement:

Select * from Tabel where Column like 'AA%B%'

But the result are:

AABCD
AABCE
AA%BC

Because the string include wildcard '%' and postgres select wrong.

Please suggest me a solution


Solution

  • To escape the % char, use \ like this \%

    UPDATE

    Another approach can be achieved by using the ESCAPE keyword with empty string after it, this way you tell postgres to disable escaping, and the % becomes a normal char

    select * from Table where Column like '%' ESCAPE '';