Wednesday, March 12, 2014

Searching Wildcard Character In a Varchar

declare @ExpressionToSearch varchar(100),@ExpressionToFind varchar(1)


set @ExpressionToSearch ='%blah hello%'
set @ExpressionToFind ='[%]'

print len(@ExpressionToSearch)
print substring(@ExpressionToSearch,1,1)
print substring(@ExpressionToSearch,2,len(@ExpressionToSearch))


print substring(@ExpressionToSearch,len(@ExpressionToSearch),1)
print substring(@ExpressionToSearch,1,len(@ExpressionToSearch)-1)


IF @ExpressionToSearch LIKE '%' + @ExpressionToFind + '%'
    PRINT 'Yes it Contains'
       if substring(@ExpressionToSearch,1,1)='%'
              print 'first'
       if substring(@ExpressionToSearch,len(@ExpressionToSearch),1)='%'
              print 'last'
ELSE
    PRINT 'It doesn''t Contain'

Links
stackoverflow
sqlhints

No comments: