Ask the Expert

Searching for rows containing underscores in SQL Server

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, hj_bus@, 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?

    Requires Free Membership to View

The underscore character is a single character wildcard in the same way the percent sign is a multi-character wild card. 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 first published in August 2007

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: