Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Like operator not working in SQL server 2019

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks

>Solution :

_ 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading