Thursday, September 15, 2016

T-SQL ESCAPE for Querying Underscores


In T-SQL the underscore is a special character when querying/filtering records. 

The following code will return all records and not only those with an underscore because the underscore acts like a wildcard if not escaped.

Select * from TableName
Where ColumnName Like '%_%'

Using ESCAPE in the query will make the special character such as the underscore a literate character.  The following code will return only those records that contain an underscore.

Select * from TableName
Where ColumnName Like '%\_% ESCAPE '\'

More information about ESCAPE

No comments:

Post a Comment