Re: Underscore "_" in LIKE pattern

From: <ludwig(at)kni-online(dot)de>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Underscore "_" in LIKE pattern
Date: 2008-05-14 10:42:25
Message-ID: 12074617.69381210761745822.JavaMail.servlet@pustefix158.kundenserver.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<span style="font-family: Verdana"> <div><font class="Apple-style-span" face="Arial">Hi Stef,<br />the underscore has to be escaped:<br /></font><span style="font-family: Verdana"><font class="Apple-style-span" face="Arial"><span class="Apple-style-span"><br />SELECT * FROM pg_tables WHERE schemaname=&#39;public&#39; AND tablename LIKE &#39;in\\_%&#39; ORDER BY tablename ASC</span></font></span><br /><span style="font-family: Verdana"><font class="Apple-style-span" face="Arial"><span class="Apple-style-span"></span></font></span><br /><font class="Apple-style-span" face="Arial"><br />Excerpt from Manual:<br /><br />To match a literal underscore or percent sign without matching other characters, the respective character<br />in pattern must be preceded by the escape character. The default
escape character is the backslash<br />but a different one can be selected by using the ESCAPE clause. To match the escape character<br />itself, write two escape characters.<br />Note that the backslash already has a special meaning in string literals, so to write a pattern constant<br />that contains a backslash you must write two backslashes in an SQL statement (assuming escape string<br />syntax is used, see Section 4.1.2.1). Thus, writing a pattern that actually matches a literal backslash<br />means writing four backslashes in the statement. You can avoid this by selecting a different escape<br />character with ESCAPE; then a backslash is not special to LIKE anymore. (But it is still special to the<br />string literal parser, so you still need two of them.)<br /><br /></font><span
style="font-family: Verdana"><font class="Apple-style-span" face="Arial"><br /> Alternative use of a regular expression:<br /> <br /> </font><span style="font-family: Verdana"><font class="Apple-style-span" face="Arial"><span class="Apple-style-span">SELECT * FROM pg_tables WHERE schemaname=&#39;public&#39; AND tablename *~ &#39;in_&#39; ORDER BY tablename ASC</span></font></span></span><br /><font class="Apple-style-span" face="Arial"><br />bye...<br />Ludwig<br type="_moz" /></font></div><div><font class="Apple-style-span" face="Arial"><span class="Apple-style-span"><br /></span></font></div> <div><font class="Apple-style-span" face="Arial"><span class="Apple-style-span"><br /></span></font></div> <div><font class="Apple-style-span" face="Arial"><span class="Apple-style-span"><br
/></span></font></div> <div><font class="Apple-style-span" face="Arial"><br /></font></div> <div><font class="Apple-style-span" face="Arial"><span class="Apple-style-span"><br /></span></font></div> </span>

Attachment Content-Type Size
unknown_filename text/html 2.5 KB

Browse pgsql-general by date

  From Date Subject
Next Message cyclix 2008-05-14 11:03:58 Re: PG -v- MySQL
Previous Message Stefan Schwarzer 2008-05-14 10:09:09 Underscore "_" in LIKE pattern