(TSQL) Escape Underscore in Like

SELECT * FROM Table WHERE Column LIKE '_a_'
The above code will generate results that has any one character before and after 'a' such as:
mac
cat
sad

SELECT * FROM Table WHERE Column LIKE '%[_]a[_]%'
The above code will generate results such as:
in_a_house
she_is_a_mother
make_a_living

3 comments:

Bill said...

Great; thank you for the help!

Rob Lang said...

Very handy, thank you.

Andrina said...

Thanks - needed this. Obvious when you know...