Click here to Skip to main content
15,072,134 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi,

I have the string below i need to check whether the my string contains square bracket or not '['.
SQL
@SEARCH_STRING = TestData[Size]

IF (@SEARCH_STRING like '[[]' )
....

i referred below link: Pattern Matching in Search Conditions[^]
Posted
Updated 25-May-15 22:10pm
v2
Comments
Maciej Los 26-May-15 4:12am
   
Based on this small piece of code we can't even guess what are you trying to achieve... Please, be more specific and provide more details.

Hi,


you can use [ ] only to find any single character within the specified range (for example, [a-f]) or set (for example, [abcdef])

this is exact answer for your requirement

SQL
DECLARE @SEARCH_STRING1 NVARCHAR(MAX) = 'I Love My [India]'

DECLARE @SEARCH_STRING2 NVARCHAR(MAX) = 'I Love My India'


IF(@SEARCH_STRING1 LIKE '%[%' )
BEGIN 
PRINT '@SEARCH_STRING1 has ['
END
ELSE 
BEGIN 
PRINT '@SEARCH_STRING1 not Contain ['
END


IF(@SEARCH_STRING2 LIKE '%[%' )
BEGIN 
PRINT '@@SEARCH_STRING2 has ['
END
ELSE 
BEGIN 
PRINT '@@SEARCH_STRING2 not Contain ['
END
   
Comments
jaket-cp 27-May-15 4:17am
   
This is what is printed when your solution is run:
@SEARCH_STRING1 not Contain [
@@SEARCH_STRING2 not Contain [

Clearly not the result you expected.
Look at solution 1 and read the link in question 'Pattern Matching in Search Conditions', section 'Searching for Wildcard Characters'
Please, read my comment to the question...

My best guess is:
SQL
DECLARE @SEARCH_STRING VARCHAR(255) = 'Whatever [!] the string is... It is for...'
DECLARE @counter INT = 0
DECLARE @stringlenght INT = LEN(@SEARCH_STRING)

WHILE (@counter<=@stringlenght)
BEGIN
    IF (SUBSTRING(@SEARCH_STRING, @counter,1)='[')
        SELECT CONCAT('A ''',  SUBSTRING(@SEARCH_STRING, @counter, 1), ''' has been found on ', CONVERT(VARCHAR(3), @counter), '. position.')
    SET @counter = @counter + 1
END


Result:
A '[' has been found on 10. position.
   
v2
Try:
SQL
...LIKE '%[[]%'...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900