backslashes in queries containing LIKE

Started by Mickael Faivre-Maconover 21 years ago3 messagesgeneral
Jump to latest
#1Mickael Faivre-Macon
faivrem@gmail.com

Hi,

We have a problem with backslashes in queries containing LIKE.

insert into table (field) values ('IM\\test')
select * from table where field = 'IM\\test'

returns one record

select * from table where field LIKE 'IM\\%'

returns no record

Why is postgres behaves like that ?
Can someone help us ?

Scripts for testing purpose have been attached.

Mickael Faivre-Macon.

Attachments:

creat_table.sqlapplication/octet-stream; name=creat_table.sqlDownload
insert_values.sqlapplication/octet-stream; name=insert_values.sqlDownload
select_values.sqlapplication/octet-stream; name=select_values.sqlDownload
#2Michael Fuhr
mike@fuhr.org
In reply to: Mickael Faivre-Macon (#1)
Re: backslashes in queries containing LIKE

On Wed, Jan 26, 2005 at 02:44:40PM +0100, Mickael Faivre-Macon wrote:

We have a problem with backslashes in queries containing LIKE.

insert into table (field) values ('IM\\test')
select * from table where field = 'IM\\test'

returns one record

select * from table where field LIKE 'IM\\%'

returns no record

See the "Pattern Matching" section in the "Functions and Operators"
chapter of the documentation:

http://www.postgresql.org/docs/8.0/static/functions-matching.html

The documentation under "LIKE" discusses issues regarding the escape
character (the backslash by default).

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

#3Mickael Faivre-Macon
faivrem@gmail.com
In reply to: Michael Fuhr (#2)
Re: backslashes in queries containing LIKE

Hi Michael,

Thank you for your reply.

We had read the LIKE doc but obviously missing the fact 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. Thus, writing a
pattern that actually matches a literal backslash means writing four
backslashes in the statement. "

Mickael.

--

See the "Pattern Matching" section in the "Functions and Operators"
chapter of the documentation:

http://www.postgresql.org/docs/8.0/static/functions-matching.html

The documentation under "LIKE" discusses issues regarding the escape
character (the backslash by default).

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/