Re: Escaping string for LIKE

From: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>
To: David Sheldon <david(at)we7(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Escaping string for LIKE
Date: 2010-11-23 20:21:20
Message-ID: AANLkTikkXgNnghPPGE5B53LKXRzY0-2ny6SsTguaukW6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hey David,

I guess, that you should escape, like this

dmitigr=> SELECT regexp_replace('di_ma%', '(%|_)', E'\\\\\\1', 'g');
regexp_replace
----------------
di\_ma\%

2010/11/23 David Sheldon <david(at)we7(dot)com>

> If I have a string, and I want to search for everything that starts with
> that string then I could do:
>
> SELECT * FROM users WHERE name LIKE string || '%';
>
> Now, if string contains \, _ or % then it won't do what I expect.
>
> Is there a built-in function that I can call to escape the string so that
> it isn't special for LIKE, or is it a case of regexing and putting a \
> before each \, _ or %?
>
> David
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

--
// Dmitriy.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2010-11-23 20:32:30 Re: Problem with replace function in postgres
Previous Message akp geek 2010-11-23 20:20:34 Re: Problem with replace function in postgres