Escaping string for LIKE

From: David Sheldon <david(at)we7(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Escaping string for LIKE
Date: 2010-11-23 11:32:25
Message-ID: 4CEBA649.4020202@we7.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Armand Turpel 2010-11-23 11:37:57 Re: json data type
Previous Message Mark Gabriel Paylaga 2010-11-23 09:42:46 Re: Connection pool