0

So my issue can basically be summarized in this small code I wrote

declare @report varchar(100) = 'fajkfgadjk'

if @report like '%_%'
begin 
    select 1
end
else 
begin
    select 2
end

It should show 2 but it is showing 1 It does not happen with others like ',&^'

Can someone help me here? I want to keep using '_'so is there any workaround?

Thanks

1 Answers1

2

_ is a wildcard character. If you want to literally match a _ in the string, you need to escape it.

IF @report like '%[_]%' -- will return true for `abc_def` but not `abcdef`

You can also use ESCAPE if the square brackets are problematic, e.g.

IF @report like '%`_%' ESCAPE '`'

See the documentation.

Aaron Bertrand
  • 261,961
  • 36
  • 448
  • 471