Good friends, I want to know how to use like inside a variable to know if the numbers 6,7,8 exist . Does anyone know what the correct syntax would be for this case?
@listaMenus = '4,5,6,7,8,9,10'
if (@listaMenus like '%8%' or '%6%')
begin
print 'no puedo continuar'
end
The closest thing to what you're asking for is using the CHARINDEX function.
Using the same example you mention, we would have the following
An interesting way is to "expand" the comma-separated list, into a table of values:
Basically we convert
@ListaMenus
to an XML and then expand each element into a new row. The result would be something like this:And now nothing prevents us from doing a
IN
on this table, for example:Important : This solution is only valid as of SQL 2008