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 [email protected], 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?

    Requires Free Membership to View

    By submitting your registration information to SearchSQLServer.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSQLServer.com is governed by our Terms of Use. You may contact us at [email protected].

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

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.