Showing posts with label compariso. Show all posts
Showing posts with label compariso. Show all posts

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

How to construct a like compariso when there are special chara

select *
from
(
select 'abc[Y]def' as c1
union all
select 'abcdef' as c1
) as t1
where c1 like '%\[Y\]%' escape ''
go
AMB
"Alejandro Mesa" wrote:
> bic,
> 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:
>"SQL Menace" <denis.gobo@.gmail.com> wrote in message
news:1149792727.734355.86450@.u72g2000cwu.googlegroups.com...
> Another way is to double the brackets
> select *
> from
> (
> select 'abc[Y]def' as c1
> union all
> select 'abcYdef' as c1
> ) as t1
> where c1 like '%[[Y]]%'
> go
>
Be careful with the bracket doubling thing - it can get confusing on complex
comparisons with lots of [ ]'s in them.