Tuesday, March 9, 2010

SQL Tip: Searching for 'special characters'

It's impossible to follow a post about the now famous, future GP guru, Aubrey, but I just wanted to share a quick SQL tip.

It seems that I rarely need to write queries that search for characters such as "_" or "%", so this morning when I had to search for all customer IDs that begin with an underscore, I couldn't remember how it was done.

A quick Google search will yield many results showing at least two simple ways to write such a query. I found this one handy:

http://sqlserver2000.databases.aspfaq.com/how-do-i-search-for-special-characters-e-g-in-sql-server.html

I personally find the 'bracket' approach to be easier to read, so with that reminder, I was able to quick produce this simple query:

SELECT * FROM RM00101 WHERE CUSTNMBR LIKE '[_]%'

No comments: