Skip to content
November 3, 2006 / dshuck

Using escape characters with MS SQL Server

This morning I was asked to do a quick and dirty query to find all rows from a table in MS SQL Server where a particular column contained one or more pecent sign (%) charaters.  Considering the ‘%’ character is a wildcard, I obviously needed an escape sequence, but oddly I had never run across this particular problem before.  After a quick search I found what appears to be a Microsoft-specific solution that suited my needs.  You can use an escape character in your ‘where’ clause, then define that character at the end of your query by adding {escape ‘[your character]‘} like this:

SELECT
MyColumn
FROM
MyTable
WHERE
MyColumn like ‘%\%%’
{escape ‘\’}

I would like to underscore that the backslash character in my statement can be whatever you need it to be in case that is another point of conflict for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 591 other followers