January 22, 2013 at 8:40 pm
Hi all,
I want to search for "[[% SQL Server %]]" pattern in a string value (Note, ] [ % are not wildcards).
But I cannot do that with LIKE keyword without ESCAPE clause, How can I fix the first query?
See:
Declare @string varchar(50) = 'abcd [[% SQL Server %]]' -- Matched
/* LIKE []: No Matched */
select case when @string like '%[[][[][%] SQL Server [%][]][]]%'
then'Matched' else 'No Matched' end
/* LIKE ESCAPE: Matched */
select case when @string like '%?[?[?% SQL Server ?%?]?]%' ESCAPE '?'
then'Matched' else 'No Matched' end
/* LEN & REPLACE: Matched */
select case when len(replace(@string,'[[% SQL Server %]]',''))<len(@string)
then'Matched' else 'No Matched' end
January 22, 2013 at 10:06 pm
Hi...its good that you spent time on explaining you requirement...but I am still having problems with understanding it, as there isn't any sample data to see what you are actually doing.
I would suggest that you post some quick sample data...maybe just 5-10 rows and then post the search output you want from the sample data. I bet we can get this working....but only if you help 🙂
January 22, 2013 at 11:34 pm
It's easy no need to sample data, I want an alternative LIKE for this without ESCAPE.
like '%?[?[?% SQL Server ?%?]?]%' ESCAPE '?'
I wrote this code, but that is not correct:
like '%[[][[][%] SQL Server [%][]][]]%'
January 23, 2013 at 2:53 am
What's wrong with using ESCAPE?
You're along the right lines with the last approach - try using CHARINDEX instead though:
SELECT CHARINDEX('[[% SQL Server %]]', @string)
January 23, 2013 at 3:50 pm
zombieisdead2020 (1/22/2013)
How can I fix the first query?
You do not need escape the closing square brackets.
This should work:
Declare @string varchar(50) = 'abcd [[% SQL Server %]]' -- Matched
/* LIKE []: No Matched */
select case when @string like '%[[][[][%] SQL Server [%]]]%'
then'Matched' else 'No Matched' end
_____________
Code for TallyGenerator
January 23, 2013 at 10:20 pm
Thanks,
So the second one can be more simpler like this:
LIKE '$[$[$% SQL Server $%]]' ESCAPE '$'
January 24, 2013 at 8:51 am
declare @search_pattern_should_be varchar(100)
set @search_pattern_should_be = '%[[][[][%] SQL Server [%]]]%'
--example
select case when @string like '%[[][[][%] SQL Server [%]]]%'
then'Matched' else 'No Matched' end
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
January 24, 2013 at 11:30 am
ScottPletcher (1/24/2013)
declare @search_pattern_should_be varchar(100)
set @search_pattern_should_be = '%[[][[][%] SQL Server [%]]]%'
--example
select case when @string like '%[[][[][%] SQL Server [%]]]%'
then'Matched' else 'No Matched' end
Is not same with Post #1410825
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply