How can you find a literal percent sign (%) in PostgreSQL using a LIKE query?

magnus picture magnus · Oct 8, 2015 · Viewed 20.6k times · Source

I have character varying entries in a table where some (not all) values contain percentages, e.g., '12%', '97%', etc. I want to find all the values that contain percentages. In other words, I want to find all values that end with a percent sign ('%').

Answer

Rahul Tripathi picture Rahul Tripathi · Oct 8, 2015

You can try like this:

SELECT * FROM my_table  WHERE my_column LIKE '%\%%' ESCAPE '\';

Format

 <like predicate> ::=
      <match value> [ NOT ] LIKE <pattern>
        [ ESCAPE <escape character> ]

 <match value> ::= <character value expression>

 <pattern> ::= <character value expression>

 <escape character> ::= <character value expression>