Question

SELECT count(*) FROM table WHERE column ilike '%/%';

gives me the number of values containing "/"

How to do the same for "\"?

Was it helpful?

Solution

SELECT  count(*)
FROM    table
WHERE   column ILIKE '%\\\\%';

OTHER TIPS

Excerpt from the docs:

Note that the backslash already has a special meaning in string literals, so to write a pattern constant that contains a backslash you must write two backslashes in an SQL statement (assuming escape string syntax is used, see Section 4.1.2.1). Thus, writing a pattern that actually matches a literal backslash means writing four backslashes in the statement. You can avoid this by selecting a different escape character with ESCAPE; then a backslash is not special to LIKE anymore. (But it is still special to the string literal parser, so you still need two of them.)

better yet - don't use like, just use standard position:

select count(*) from from table where 0 < position( E'\\' in column );

You need E'\\\\' because the argument to LIKE is a regex and regex escape char is already \ (e.g ~ E'\\w' would match any string containing a printable char).

See the doc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top