Little SQL Server Tricks: Use Underline Characters With Like

I needed to find all columns with an underscore (_) in their name and came up with this query:

But instead of showing me columns with a _ in their name, I got every column back that exists in my database. When I replace _ with anything else, I got the expected result. What is so special with the _ character?

As it turns out, the _ is a wildcard to match one character in SQL Server – as it is in Oracle. To get the result I wanted, I had to escape the _ character and rewrite my where-clause to this:

If you are used to regular expressions, you may find this alternative useful:

With this small change, I finally got the result I was expecting.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.