Search Postgresql Archives

Re: Underscore "_" in LIKE pattern

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Stef,
the underscore has to be escaped:

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



Excerpt from Manual:

To match a literal underscore or percent sign without matching other characters, the respective character
in pattern must be preceded by the escape character. The default escape character is the backslash
but a different one can be selected by using the ESCAPE clause. To match the escape character
itself, write two escape characters.
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.)


Alternative use of a regular _expression_:

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


bye...
Ludwig






[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux