Problem solve Get help with specific problems with your technologies, process and projects.

Is it possible to search for rows containing underscores in SQL Server?

Is it possible to do a SQL Server search for a row with particular strings containing an underscore? Learn how to structure the WHERE clause to search for such rows.

Is it possible to search for a row or rows with particular strings containing an underscore?

Suppose I have various email addresses all containing an underscore like gm_ace@where.com, hj_bus@ where.com, etc.How can I search for those particular emails?

If I use a WHERE clause such as 'WHERE eMail LIKE '%_' it returns all rows. 'WHERE eMail LIKE 'gm_%' returns rows that do not contain an underscore. Can you offer any suggestions?

The underscore character is a single character wildcard in the same way the percent sign is a multi-character wildcard. To query for an underscore character you need to wrap the underscore in square brackets:

WHERE email LIKE 'gm[_]%'

If you want to search for all email addresses with a two character value then an underscore, this WHERE clause would work:

WHERE email like '__[_]%@%'

This was last published in August 2007

Dig Deeper on Microsoft SQL Server Performance Monitoring and Tuning

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.