LIKE - finding ']' or '.' in data

  • Hi,

    In the code below I am trying to find all of the values with '.123' or ']123' within them. Obviously I could use two LIKE statements like I have below, but ideally I'd prefer to use a single LIKE statment.

    DECLARE @Table1 TABLE

    (

    Column1 VARCHAR(32) NOT NULL PRIMARY KEY

    );

    INSERT @Table1(Column1)

    VALUES

    ('abcdef123'),

    ('abcdef]123'),

    ('abcdef].123'),

    ('abcdef)123'),

    ('abcdef.123');

    SELECT T.Column1

    FROM @Table1 T

    WHERE T.Column1 LIKE '%]123%'

    OR T.Column1 LIKE '%.123%';

    If we were dealing with any normal brackets instead of square brackets, we could use:

    LIKE '%[).]123%'

    But this doesn't work with ']' due to it finishing off the choice functionality of the LIKE.

    Does anyone have any suggestions on how I can include the ']' character within the LIKE '%[]%' functionality?

  • did you try to double up the bracket you're looking for?

    '%[]]%'

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • You could use an ESCAPE character.

    DECLARE @Table1 TABLE

    (

    Column1 VARCHAR(32) NOT NULL PRIMARY KEY

    );

    INSERT @Table1(Column1)

    VALUES

    ('abcdef123'),

    ('abcdef]123'),

    ('abcdef].123'),

    ('abcdef)123'),

    ('abcdef.123');

    SELECT T.Column1

    FROM @Table1 T

    WHERE T.Column1 LIKE '%\]123%' ESCAPE '\'

    OR T.Column1 LIKE '%.123%';

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • apparently the regex should be like this:

    DECLARE @Table1 TABLE

    (

    Column1 VARCHAR(32) NOT NULL PRIMARY KEY

    );

    INSERT @Table1(Column1)

    VALUES

    ('abcdef123'),

    ('abcdef[123'),

    ('abcdef]123'),

    ('abcdef].123'),

    ('abcdef)123'),

    ('abcdef.123');

    SELECT T.Column1

    FROM @Table1 T

    WHERE T.Column1 LIKE '%[).[]123%'

    order by Column1;

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Luis Cazares solution works for me. Thanks!

    Luis Cazares (12/2/2013)


    You could use an ESCAPE character.

    End SQL:

    DECLARE @Table1 TABLE

    (

    Column1 VARCHAR(32) NOT NULL PRIMARY KEY

    );

    INSERT @Table1(Column1)

    VALUES

    ('abcdef123'),

    ('abcdef]123'),

    ('abcdef[123'),

    ('abcdef].123'),

    ('abcdef)123'),

    ('abcdef.123');

    SELECT T.Column1

    FROM @Table1 T

    WHERE T.Column1 LIKE '%[.\]]123%' ESCAPE '\';

  • Luis' solution is right.

    You can always ESCAPE any character you want, just be sure that it does not appear in your string.. "\" in your case ... or you could use something like "!" or "$" as long as you know that it won't be in any of your column's values..

    "The price of anything is the amount of life you exchange for it" - Henry David Thoreau

Viewing 6 posts - 1 through 5 (of 5 total)

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