Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

LIKE - finding ']' or '.' in data Expand / Collapse
Author
Message
Posted Monday, December 2, 2013 9:31 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:10 AM
Points: 258, Visits: 318
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?
Post #1518935
Posted Monday, December 2, 2013 10:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:47 AM
Points: 7,005, Visits: 8,451
did you try to double up the bracket you're looking for?

'%[]]%'


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

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


- 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
Post #1518946
Posted Monday, December 2, 2013 10:18 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 8:30 PM
Points: 3,541, Visits: 7,636
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1518947
Posted Tuesday, December 3, 2013 3:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:47 AM
Points: 7,005, Visits: 8,451
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


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

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


- 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
Post #1519137
Posted Tuesday, December 3, 2013 3:57 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:10 AM
Points: 258, Visits: 318
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 '\';

Post #1519155
Posted Saturday, December 28, 2013 3:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:06 PM
Points: 6, Visits: 39
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..
Post #1526281
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse