Introduction:

Here we will explain how to use Like clause in SQL Server or what is Like clause in SQL Server or how to filter record using Like clause in SQL Server with Example.


Description:

The Like clause is used to return all rows in a table whose column values match a specified pattern.it is useful when you want to search rows to match a specific pattern in a column, or when you do not know the entire value of a column. For this purpose we use a wildcard character '%'.


  • LIKE Syntax
SELECT column1, column2 FROM table_name
WHERE columnN LIKE pattern;

 

The Following SQL Statement selects all Records from Student Table.

select * from Student

Result:



  • SQL LIKE Examples

 

The following SQL statement selects all students with student Name starting with "a":

SELECT * FROM Student
WHERE Name LIKE 'a%';


The following SQL statement selects all students with student Name ending with "a":

SELECT * FROM Student
WHERE Name LIKE '%a';


The following SQL statement selects all students with student Name that have "a" in any position:

SELECT * FROM Student
WHERE Name LIKE '%a%';


The following SQL statement selects all students with student Name that have "a" in the second position:

SELECT * FROM Student
WHERE Name LIKE '_a%';


The following SQL statement selects all students with student Name that starts with "a" and are at least 3 characters in length:

SELECT * FROM Student
WHERE Name LIKE 'a_%_%';


The following SQL statement selects all students with student Name that starts with "a" and ends with "r":

SELECT * FROM Student
WHERE Name LIKE 'a%r';


The following SQL statement selects all students with student Name that NOT starts with "a":

SELECT * FROM Student
WHERE Name NOT LIKE  'a%';




I hope this article will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.