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 ('%'
).
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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>
回答2:
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
回答3:
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.