How to escape Wildcards when searching for [SS]?

  • Hi,

    I want to find all records where a field contains the following literal text

    [SS]

    the above four characters can be anywhere in the string. Can anyone advise how to escape from the typical behaviour of square brackets as wildcards. I've tried various ways of using the ! escape character, and google/BOL to no avail.

    Any help greatly appreciated.

  • DECLARE @t TABLE (Data VARCHAR(30))

    INSERT @t (Data)

    VALUES ('abcssdef'),

    ('abc[ss]def')

    select * from @t

    where data like '%[[ss]]%'

  • Thanks for the reply, but that doesn't quite work. Try this, and you'll see what I mean:

    create table #t (Data VARCHAR(30))

    INSERT #t (Data)

    VALUES ('abcssdef'),

    ('abc[ss]def'),

    ('abc[70ps]def')

    select * from #t

    where data like '%[[ss]]%'

  • Shoot, you are right, sorry about that. How about this?

    DECLARE @t TABLE (Data VARCHAR(30))

    INSERT @t (Data) VALUES

    ('abcssdef'),

    ('abc[ss]def'),

    ('abc[70ps]def')

    select * from @t

    where data like '%\[ss\]%' escape '\'

  • That's it! thanks.

    I've had to shuttle the data into tempdb on one of our 2005 servers though to get this to work.

    Do you know if this is even possible on SS2000 without using ESCAPE?

    Regards . . .Jason

  • Boy, it is too early in the morning for me. I didn't even realize this was the 2000 board. D'oh. I don't know of anything off the top of my head that will get that without the escape clause so I will wait and see if someone else can chime in on that before I can come up with something.

  • Wait, try this in the where clause:

    where data like '%[[]ss]%'

  • That's the one!!

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply