Underscore "_" in LIKE pattern

Started by Stefan Schwarzeralmost 18 years ago2 messagesgeneral
Jump to latest
#1Stefan Schwarzer
stefan.schwarzer@grid.unep.ch

Hi there,

I would like to get a list of all tables in my schema which begin with
"in_". But as the "underscore (_) in pattern stands for (matches) any
single character", my query:

SELECT * FROM pg_tables WHERE schemaname='public' AND tablename LIKE
'in_%' ORDER BY tablename ASC

doesn't exactly do what I want.

It retrieves me tables like this one (which is what I want):

in_water_use_ind

but also tables like this one (which is not what I want):

ind_gdp

I am sure this is just a minor thing. But I have no idea how to solve
it.

Thanks for any hints!

Stef

#2Ludwig Kniprath
ludwig@kni-online.de
In reply to: Stefan Schwarzer (#1)
Re: Underscore "_" in LIKE pattern

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