How to escape % sign from like command

Escape % sign Of Like Command
Today i will discuss how to use like command with % however % is already exist in out table column.I am not discuss here about like command i just focus on heading of this article.
In SQL Server like command is basically used for use wild card character in where clause.The most useful wild character is % . But what happen when in our column already contains % in column data.Have a look for example.In this example i created the case where we can easily understand about this scenario.

Sql server escape % sign

DECLARE @ProductTotals TABLE
  NAME varchar(100)
INSERT INTO @ProductTotals(NAME)

-- I want to fetch all record which have c%h
SELECT * FROM @ProductTotals WHERE NAME like '%c%h%'

SELECT * FROM @ProductTotals WHERE NAME like '%c[%]h%'
-- if we exeucte first query then it return all record but when we
-- execute second one then its give exact result.

Output of above Example

Escape % sign from like command
 Use the [] square bracket to get the required result.


Post a Comment