13.4.09

Pattern matching in SQL Server

Pattern matching is a very important topic in SQL Server. It enable us to get the relative or exact pattern of data from the database. Pattern matching can be easily achieved through LIKE operator in the where clause.

% - Operator is used to fetch 0 or any number of characters and
_ - Operato is used to identify one character at its place. L

Lets see some example to learn more,
-- Creating a table and inserting some dummy data into that table.
drop table venkat
create table venkat(id int, name varchar(10))
insert into venkat
select 1,'Venkat'
union all
select 2,'arun'
union all
select 3,'Lakshmi'
union all
select 4,'arun'
union all
select 5,'ve'
--String Starts with "V" and having any number of characters. % indicates any number of character
select * from venkat where name like 'V%'
--String Starts with "V" and having only one character. _ indicates only one number.
select * from venkat where name like 'V_'
--String should not start with "V"
select * from venkat where name not like 'V%'
Thanks and Regards,
Venkatesan Prabu .J

No comments:

Post a Comment