0

Using escape characters with MS SQL Server

SQL

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.

tags:
SQL
fro said:
 
Interesting post Dave. Since I deal a lot with Oracle at work, I thought I'd see if it has an escape method. It sure does. Here is your example in Oracle:

SELECT my_column
FROM my_table
WHERE my_column LIKE '%\%%'
ESCAPE '\'

Very similar.
 
posted 663 days ago
Add Comment Reply to: this comment OR this thread
 
jen said:
 
thanks! i have actually been trying to figure this one out!
 
posted 658 days ago
Add Comment Reply to: this comment OR this thread
 
 
Anytime! Glad to help.
 
posted 658 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Dave Shuck for President!