|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 5:29 AM
Points: 18,
Visits: 66
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 3:06 AM
Points: 1,051,
Visits: 1,442
|
|
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
Vinu Vijayan
For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 5:29 AM
Points: 18,
Visits: 66
|
|
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 [%][]][]]%'
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:53 AM
Points: 1,474,
Visits: 2,342
|
|
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)
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 6:59 PM
Points: 4,557,
Visits: 8,215
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 5:29 AM
Points: 18,
Visits: 66
|
|
Thanks, So the second one can be more simpler like this:
LIKE '$[$[$% SQL Server $%]]' ESCAPE '$'
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 3:56 PM
Points: 1,324,
Visits: 1,778
|
|
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) One man with courage makes a majority. Andrew Jackson
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, February 05, 2013 5:29 AM
Points: 18,
Visits: 66
|
|
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
|
|
|
|