In SQL. Why does this 'Like' statement with a wildcard not work?

  • A+
Category:Languages
Declare @Temp1 as nvarchar(max) = '[10004120][1100][10033583][1005]' Declare @Temp2 as nvarchar(max) = '[10004120][1100]'  If @Temp1 like @Temp2 + '%'     Print 'Yup' 

Why does this not work? I don't get the "yup" message.

 


This doesn't work because the brackets in the string have a special function in a LIKE statement - items between the brackets constitute a set of values that the singular character at the specified position matches. Your original pattern looks for a 1,0,4, or 2 followed by a 1 or an 0. To make this work, you should have a pattern like this:

Declare @Temp2 as nvarchar(max) = '[[]10004120][[]1100]' 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: