Re: Underscore "_" in LIKE pattern - Mailing list pgsql-general

From
Subject Re: Underscore "_" in LIKE pattern
Date
Msg-id 12074617.69381210761745822.JavaMail.servlet@pustefix158.kundenserver.de
Whole thread Raw
In response to Underscore "_" in LIKE pattern  (Stefan Schwarzer <stefan.schwarzer@grid.unep.ch>)
List pgsql-general
Hi Stef,the underscore has to be escaped:SELECT * FROM pg_tables WHERE schemaname='public' AND tablename LIKE
'in\\_%'ORDER BY tablename ASCExcerpt from Manual:To match a literal underscore or percent sign without
matchingother characters, the respective characterin pattern must be preceded by the escape character. The default 
escape character is the backslashbut a different one can be selected by using the ESCAPE clause. To match the escape
characteritself,write two escape characters.Note that the backslash already has a special meaning in string literals,
soto write a pattern constantthat contains a backslash you must write two backslashes in an SQL statement (assuming
escapestringsyntax is used, see Section 4.1.2.1). Thus, writing a pattern that actually matches a literal
backslashmeanswriting four backslashes in the statement. You can avoid this by selecting a different escapecharacter
withESCAPE; then a backslash is not special to LIKE anymore. (But it is still special to thestring literal parser, so
youstill need two of them.)<span 
style="font-family: Verdana"> Alternative use of a regular expression:  SELECT * FROM pg_tables WHERE
schemaname='public'AND tablename *~ 'in_' ORDER BY tablename ASCbye...Ludwig  <br 
/>

pgsql-general by date:

Previous
From: Stefan Schwarzer
Date:
Subject: Underscore "_" in LIKE pattern
Next
From: cyclix
Date:
Subject: Re: PG -v- MySQL