LIKE operator and string comparison

LIKE operator and string comparison

am 23.09.2004 00:54:46 von wweng

I used the following SQL code to match '\foo\bar'

SELECT text FROM test WHERE text LIKE '\\\\foo\\\\%'

But if I choose to use string comparison, instead of 4 escape characters, I
only need 2.

SELECT text FROM test WHERE text = '\\foo\\bar'

Why is that?

I am using PostgreSQL 7.4, and the SQL code was entered through psql.

Thanks!

Wei

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Re: LIKE operator and string comparison

am 23.09.2004 01:09:44 von tgl

Wei Weng writes:
> But if I choose to use string comparison, instead of 4 escape characters, I
> only need 2.

> Why is that?

Backslash is an escape character for LIKE.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Re: LIKE operator and string comparison

am 23.09.2004 01:19:30 von wweng

Tom Lane wrote:

> Wei Weng writes:
>
>>But if I choose to use string comparison, instead of 4 escape characters, I
>>only need 2.
>
>
>>Why is that?
>
>
> Backslash is an escape character for LIKE.
>
> regards, tom lane
>
>

What about in regular strings? I do need to use backslash to escape the
other backslash in order to get the '\'. Why isn't that the case in LIKE
operation?

Thanks

Wei


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Re: LIKE operator and string comparison

am 23.09.2004 01:20:48 von tgl

Wei Weng writes:
> What about in regular strings? I do need to use backslash to escape the
> other backslash in order to get the '\'. Why isn't that the case in LIKE
> operation?

It is. You write four backslashes in order to describe a string
constant value containing two backslashes. When the LIKE code sees
that, it interprets it as one quoted backslash.

See LIKE ... ESCAPE if you'd like to use a different escape character
in the LIKE pattern.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly