December 2, 2013 at 9:31 am
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?
December 2, 2013 at 10:16 am
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
December 2, 2013 at 10:18 am
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%';
December 3, 2013 at 3:08 am
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
December 3, 2013 at 3:57 am
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 '\';
December 28, 2013 at 3:50 am
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..
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply