Sql Like Operator

SQL LIKE OPERATOR:-  The SQL LIKE operator is used to find matches between a character string and a specified pattern . This operator is used in conjunction with the SELECT statement and WHERE clause.
 There are two wildcards often used in conjunction with the LIKE operator.
  •  %  = The percentage sign represents zero, one, or multiple characters.
  •  _   =  The underscore represents a single character.
you can combine any number of conditions using AND, OR operators.

SYNTAX:-

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

EXAMPLE:-
consider the STUDENT table having the records as shown below:-

SQL LIKE Examples:-
  1. The following SQL statement selects all students with a Name starting with "K":-
SELECT * FROM STUDENT
WHERE Name LIKE  'K%' ;

OUTPUT:-

    2. The following SQL statement selects all students with a Name ending with "a":-

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

OUTPUT:-

   3. The following SQL statement selects all students with a Name that have "om" in any position:-

SELECT * FROM STUDENT
WHERE Name LIKE '%om%' ;

OUTPUT:-

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

SELECT * FROM STUDENT 
WHERE Name LIKE  '_r%' ;

OUTPUT:-

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

SELECT * FROM STUDENT 
WHERE Name LIKE 'T__%' ;   

OUTPUT:-

   6. The following SQL statement selects all students with a Name that starts with "N" and ends with "a"  :-

SELECT * FROM STUDENT 
WHERE Name LIKE  'N%a';    

OUTPUT:-

   7.  The following SQL statement selects all students with a Name that does NOT start with "K":-

SELECT * FROM STUDENT 
WHERE Name NOT  LIKE 'K%'

OUTPUT:-

These are the some variations of the LIKE operator.


Comments

Popular posts from this blog

ASP.NET Overview

SQL AND ,OR, NOT Operators

SQL Joins