Re: BUG #7835: The _ acts like a wildcard when used as '%_%'

From: Jaime Casanova <jaime(at)2ndquadrant(dot)com>
To: elliott(dot)groszek(at)navy(dot)mil
Cc: pgsql-bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #7835: The _ acts like a wildcard when used as '%_%'
Date: 2013-01-29 18:14:43
Message-ID: CAJKUy5i3D78MeYLFUfmditpo7n3TXYM1fVci3=oahy_YBDRR0g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, Jan 29, 2013 at 12:20 PM, <elliott(dot)groszek(at)navy(dot)mil> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 7835
> Logged by: Elliott Groszek
> Email address: elliott(dot)groszek(at)navy(dot)mil
> PostgreSQL version: 9.0.11
> Operating system: Linux
> Description:
>
> Using the _ (underscore) in a wildcard query accesses values with - (dash)
> as well. This results in unexpected behaviors when some data values contain
> the underscore and some data values contain the dash.
>

i guess you are using a LIKE expression. And in like both % and _ are
wildcards, as documented in:
http://www.postgresql.org/docs/9.2/static/functions-matching.html#FUNCTIONS-LIKE
And AFAIU, mandated by SQL Standard

"An underscore (_) in pattern stands for (matches) any single
character; a percent sign (%) matches any sequence of zero or more
characters."

if you only want to show those that contains an underscore (supressing
its wildcard behaviour) you need to use a escape character:

col LIKE '%\_%'

--
Jaime Casanova www.2ndQuadrant.com
Professional PostgreSQL: Soporte 24x7 y capacitación
Phone: +593 4 5107566 Cell: +593 987171157

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message kurt.lidl 2013-01-29 18:20:05 BUG #7836: COPY command does not honor 'FORMAT' option
Previous Message elliott.groszek 2013-01-29 17:20:26 BUG #7835: The _ acts like a wildcard when used as '%_%'