SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
andrew.manning
andrew.manning
Old Hand
Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)

Group: General Forum Members
Points: 348 Visits: 387
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?
ALZDBA
ALZDBA
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30243 Visits: 8986
did you try to double up the bracket you're looking for?

'%[]]%'

Johan


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


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


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42428 Visits: 19838
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
ALZDBA
ALZDBA
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30243 Visits: 8986
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


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


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


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
andrew.manning
andrew.manning
Old Hand
Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)

Group: General Forum Members
Points: 348 Visits: 387
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 '\';


Mohit Dhiman
Mohit Dhiman
Old Hand
Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)

Group: General Forum Members
Points: 381 Visits: 267
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search