pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: brianb-pgsql@edsamail.com
Cc: pgsql-sql@postgresql.org, Bruce Momjian <pgman@candle.pha.pa.us>
Subject: Re: Search for underscore w/ LIKE
Date: Fri, 07 Jul 2000 13:33:58 -0400
Message-ID: <10965.962991238@sss.pgh.pa.us> (raw)
In-Reply-To: <20000707090925.3041.qmail@mail01.edsamail.com.ph>
References: <20000707090925.3041.qmail@mail01.edsamail.com.ph>

brianb-pgsql@edsamail.com writes:
> How do I use LIKE to search for strings with an underscore? The
> documentation (well, Bruce's book) says to use 2 underscores (__) but it
> doesn't work.

If Bruce's book says that, I hope it's not too late for him to change
it ;-)

The correct way is to escape the underscore with a backslash.  You
actually have to write two backslashes in your query:

	select * from foo where bar like '%\\_baz'

The first backslash quotes the second one for the query parser, so that
what ends up inside the system is %\_baz, and then the LIKE function
knows what to do with that.

Similarly, '\\%' would be the way to match a literal %.  You can
actually backslash-quote any single character this way in LIKE,
but % and _ are the only ones where it makes a difference.  (In the
regexp-matching operators there are many more special characters and
so many more times when you need the backslash trick.)

			regards, tom lane



view thread (5+ messages)  latest in thread

Message-ID: <10965.962991238@sss.pgh.pa.us>
Permalink:  ../10965.962991238@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10965.962991238@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, brianb-pgsql@edsamail.com, pgman@candle.pha.pa.us
  Subject: Re: Search for underscore w/ LIKE
  In-Reply-To: <10965.962991238@sss.pgh.pa.us>
  References: <20000707090925.3041.qmail@mail01.edsamail.com.ph> <10965.962991238@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox