Monday, 11 February 2013

How to use LIKE Operator in SQL Server

SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column. We can use the Like operator with the combination of Where clause to filter the specified pattern data from a Table.

Syntax:
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern

Example :
Suppose we have an Employee Table as follows :

----------------------------------------------------------------------------------------------------------
The Wild Card '%' Usage with LIKE :
----------------------------------------------------------------------------------------------------------
Criteria-I:
To select Employees whose name Starts with Ravi
Select *from EMPLOYEE E WHERE E.E_Name LIKE 'ravi%'

The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.


Result:

----------------------------------------------------------------------------------------------------------
Criteria-II:
To select Employees whose name Ends with Reddy
Select *from EMPLOYEE E WHERE E.E_Name LIKE '%reddy'

Result:
----------------------------------------------------------------------------------------------------------
Criteria-III:
To select Employees whose name contains 'Kumar'
Select *from EMPLOYEE E WHERE E.E_Name LIKE '%kumar%'

Result:

----------------------------------------------------------------------------------------------------------
Criteria-IV:
To select Employees whose name NOT contains 'Reddy'
Select *from EMPLOYEE E WHERE E.E_Name NOT LIKE '%reddy%'


Result:

Thanks,TAMATAM

No comments:

Post a Comment

Hi User, Thank You for Visiting My Blog. Please Post Your Feedback/Comments/Query.

Subscribe to Blog Posts by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts