Table of Contents
MS SQL Server: Condition LIKE in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The LIKE condition in SQL Server (Transact-SQL) allows the use of wildcards in the WHERE clause in SELECT, INSERT, UPDATE and DELETE statements, used to match patterns.
Syntax LIKE condition
'bi?u th?c' LIKE 'm?u' [ ESCAPE 'Escape_Character' ]
Variable name or variable value
expression
Character expressions such as columns or information fields.
form
Character formats contain collated samples. Samples can be selected from the table below.
Representative symbol Explain% finding the character string for any length (including length 0) _ finding a character [^] to find any character not contained in the [^] character (eg [ ^ abc] will find any character that is not a, b or c)Escape Character
Option. Temporarily translates as a skip character, allowing you to check if the characters like% or _ are used literally (in the form of constants), but not as a special character.
As an example, - use wildcard%
Use the wildcard% in SQL Server's LIKE condition, As an example, by looking for employees who have them starting with the letter 'B'
SELECT *FROM nhanvienWHERE ho LIKE 'B%';
You can use% characters in the same string as the example below.
SELECT *FROM nhanvienWHER ho LIKE '%0%';
The results for the employees in which they contain the letter 'o'.
As an example, - use wildcard _
Note that the character _ is only used to search for 1 character (length is 1).
SELECT *FROM nhanvienWHERE ho LIKE 'Ad_m';
In this example, the returned result is the employee with a 4-letter name, where the first two letters are 'Ad' and the last letter is 'm'. As an example,, Adam, Adem, Adim, Adom.
This is another example
SELECT *FROM nhanvienWHERE so_nhanvien LIKE '123_';
You are looking for the number of employees that only 3 in 4 digits. In the above example, the result returned includes 10 records, missing values are from 0 to 9: 1230, 1231, 1232.
As an example, - use wildcard []
Note that what's enclosed in square brackets is the character you want and match the pattern.
SELECT *FROM nhanvienWHERE ten LIKE 'Sm[iy]th';
The result returned in this example is the employees whose name has 5 letters, of which the first two letters are 'Sm' and the last two letters are 'th', the middle letter can be 'i' or 'y'. So the result might be Smith or Smyth.
As an example, - use wildcard [^]
What is in square brackets are the characters you don't want to match the pattern.
SELECT *FROM nhanvienWHERE ten LIKE 'Smy[^iy]th';
As a result, the employee name has 5 letters, in which the first two letters are 'Sm', the last two words are 'th' and the middle word is not 'i' nor 'y'. The result could be Smath, Smeth.
As an example, - use the NOT operator
This is how to use the NOT operator in SQL Server with wildcards. You can find the employee surname that does not start with the letter 'B' with the LIKE condition.
SELECT *FROM nhanvienWHERE ho NOT LIKE 'B%';
By setting the NOT operator before the LIKE condition, you find the employee family that does not start with 'B'.
As an example, - use the character to skip Escape Character
Using skip characters is important when comparing patterns, to avoid misunderstanding special characters when you want to use it as a constant value.
As an example,, if you want to search for characters a% or a_ As an example,.
Note that only the characters can be defined by 1 character (length is 1).
SELECT *FROM nhanvienWHERE secret_hint LIKE '123!%455' ESCAPE '!';
This command will return employees whose secret_hint is 123% 455. The% character is no longer used with the previous meaning but as a normal character.
This is a more complex example.
SELECT *FROM nhanvienWHERE secret_hint LIKE 'H%!%' ESCAPE '!';
The return result of the LIKE condition is that the employees with secret_hint start with 'H' and end with '%', such as 'Help%'.
FAQ
What is MS SQL Server: Condition LIKE in SQL Server?
The LIKE condition in SQL Server (Transact-SQL) allows the use of wildcards in the WHERE clause in SELECT, INSERT, UPDATE and DELETE statements, used to match patterns.
Why is MS SQL Server: Condition LIKE in SQL Server important?
A clear understanding of MS SQL Server: Condition LIKE in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach MS SQL Server: Condition LIKE in SQL Server?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Reader Comments 0
Sign in with email or Google to join the discussion.