Tuesday, August 14, 2018

How to find underscore(_) using like operator in SQL Server


Like operator is used to match the specified pattern.
I came across the below scenario where i need to search the underscore using like operator.
Here is the example for title table

select Name from title where Name like 'santosh%'

Name
Santosh
Santosh_Kumar
Santoshkumar

select Name from title where Name like 'santosh_%'

Name
Santosh_Kumar
Santoshkumar

But I need the output only Santosh_Kumar.
So modify the SQL script like below. and output will be as expected.

select Name from title where Name like 'santosh[_]%'

Or use ESCAPE Operator

select Name from title where Name like 'santosh\_%' escape '\'

Name
Santosh_Kumar

No comments:

Post a Comment