tags:

views:

264

answers:

4

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

gives me the number of values containing "/"

How to do the same for "\"?

+2  A: 

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.)

Milen A. Radev
+3  A: 
SELECT  count(*)
FROM    table
WHERE   column ILIKE '%\\\\%';
Quassnoi
A: 

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

Benoît
+1  A: 

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

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