I am trying to catch an error in a T-SQL variable name by making sure that the variable value is prefixed with the brackets "[".
Here is an example of how I am trying to do this:
DECLARE @thing nvarchar(20) SET @thing = '[55555' IF(@thing NOT LIKE '[' + '%') --If the value does not start with [ then add it BEGIN SET @thing = '[' + @thing END PRINT @thing
Example above PRINT [[55555
Note that the original @thing value was prefixed with the brackets "[". I expected the IF condition to return false since "[55555" is LIKE '[' + '%'
Why doesn't the IF condition return false? And, more importantly, I believe that the correct syntax is to check for the existence of the string that occurs at the beginning of the value of the string variable?
EDIT It looks like there is something special in the bracket. When I run LIKE on a bracket, it does not do what I expect, but when I do not use a bracket, LIKE works as I expect.
Check out these examples:
IF('[' NOT LIKE '[') BEGIN PRINT '[ is NOT LIKE [' END ELSE BEGIN PRINT '[ is LIKE [' END IF('StackO' NOT LIKE 'StackO') BEGIN PRINT 'STACKO is NOT LIKE StackO' END ELSE BEGIN PRINT 'STACKO is LIKE StackO' END
Here is the conclusion of two conditions:
[I DO NOT LIKE [
STACKO LIKE StackO
source share