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

六眼飞鱼酱① 提交于 2019-12-04 00:33:51

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>

You have to escape the literal % sign. By default the escape character is the backslash:

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

In this case the first % sign matches any starting sequence in my_column. The remaining \% are interpreted as a literal % character. The combination is therefore: match anything that ends in a % character.

SQLFiddle

I ended up using regular expressions:

select * from my_table where my_column ~ '%$';

However, I'd still like to know if it's possible using the LIKE operator/comparison.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!