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

No Wildcards Characters Expand / Collapse
Author
Message
Posted Tuesday, January 22, 2013 8:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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

Post #1410319
Posted Tuesday, January 22, 2013 10:06 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen 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
Post #1410339
Posted Tuesday, January 22, 2013 11:34 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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 [%][]][]]%'
Post #1410356
Posted Wednesday, January 23, 2013 2:53 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP 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)
Post #1410430
Posted Wednesday, January 23, 2013 3:50 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal 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

Post #1410825
Posted Wednesday, January 23, 2013 10:20 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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 '$'
Post #1410893
Posted Thursday, January 24, 2013 8:51 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen 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
Post #1411185
Posted Thursday, January 24, 2013 11:30 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Post #1411277
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse