Wednesday, March 28, 2012

How to construct a like compariso when there are special characts

How do I construct a like compariso query when there are special characts
such as [ and ] in the string, i.e. [Y] to be searched? Thanks.
--
bicbic,
use a scape character.
Example:
select *
from
(
select 'abc[Y]def' as c1
union all
select 'abc[Y]def' as c1
) as t1
where c1 like '%\[Y\]%' escape ''
go
AMB
"bic" wrote:

> How do I construct a like compariso query when there are special characts
> such as [ and ] in the string, i.e. [Y] to be searched? Thanks.
> --
> bic|||You can use the ESCAPE clause. In this example I'm using '!' as the escape
character:
DECLARE @.x VARCHAR(100)
SELECT @.x = '[Y]'
SELECT
CASE
WHEN @.x LIKE '![Y!]' ESCAPE '!' THEN 'Match'
ELSE 'No Match' END AS Answer
"bic" <bic@.discussions.microsoft.com> wrote in message
news:B548D713-0022-47D1-A0E4-47630561159C@.microsoft.com...
> How do I construct a like compariso query when there are special characts
> such as [ and ] in the string, i.e. [Y] to be searched? Thanks.
> --
> bic

No comments:

Post a Comment