Back to list SQL Server How to Replace Part of a String in T-SQL Database: SQL Server Operators:LIKE, WHERE Problem: You’d like to select rows that contain a string starting with one of a given group of characters. Example: Our database has a table named gamer with data in the id and user columns. iduser 1superman 2magnus 3lucy 4stan 5ali Let’s find all the data for gamers whose user names start with ‘a’, ‘b’, ‘r’, or ‘s’. Solution: SELECT id, user FROM gamer WHERE user LIKE '[abrs]%'; Here is the result: iduser 1superman 4stan 5ali Discussion: If you want to filter rows containing a string that matches a given pattern, use the WHERE clause with the LIKE operator. The WHERE clause is used to filter records that meet stated conditions. After WHERE, you add a column name or an expression, followed by the operator (e.g. LIKE) and the comparison value(s). After LIKE, you enter the pattern to match (in our example, WHERE user LIKE '[abrs]%'). The pattern can contain characters and wildcards. One of T-SQL’s wildcards is ‘%’, which represents zero or more unknown characters. The underscore wildcard ( _ ) indicates one character. The brackets (  ) enclose the pattern to match; this pattern can contain one or more letters or numbers. The target string can match one or more of the characters in the brackets. In our example, the pattern '[abrs]%' denotes that the user name has to start with either a, b, r, or s. After the starting character, there can be zero or more characters because we’ve used the % wildcard. The users with id=2 and id=3 are not displayed; their names start with ‘l’ and ’m’ and thus don’t match the pattern we’ve selected. Recommended courses: SQL Basics in SQL Server SQL Practice Set Common Functions in SQL Server Recommended articles: How to Use LIKE in SQL: SQL Pattern Matching How to Use a SQL Wildcard Performing Calculations on Date- and Time-Related Values How Often Employees Are Running Late for Work: SQL Datetime and Interval SQL Arithmetic See also: How to Replace Part of a String in T-SQL How to Find the Number of Days Between Two Dates in MySQL How to Find the Last Day of the Month for a Given Date in MySQL How to Change Seconds to a Time Value in MySQL How to Change Datetime Formats in MySQL How to Get the Date from a Datetime Column in MySQL How to Get the Current Date and Time in MySQL How to Add Days to a Date in MySQL How to Add Time to a Datetime Value in MySQL Subscribe to our newsletter Join our weekly newsletter to be notified about the latest posts.