Thursday, 5 March 2015

How we can perform a limited character’s search using LIKE?

How do I perform a limited character’s search using LIKE?


Declare @T TABLE (Id Int, Name Varchar(100))
Insert Into @T Values (1,'abc1'),(2,'def2'), (3,'ghi'), (4,'zyx'),(5,'123')

--Select * from @T
--WE WILL GET RESULTS FOR ONLY a TO b
Select * from @T
where Name like '%[a-b]%' COLLATE Latin1_General_CS_AS

--WE WILL GET RESULTS FOR ONLY d TO f
Select * from @T
where Name like '%[d-f]%' COLLATE Latin1_General_CS_AS

--WE WILL GET RESULTS FOR ONLY a TO z
Select * from @T
where Name like '%[a-z]%' COLLATE Latin1_General_CS_AS

--WE WILL GET RESULTS FOR BOTH COLUMNS WITH NUMERIC AND ALPHABETS
Select * from @T
where Name like '%[a-b0-9]%' COLLATE Latin1_General_CS_AS

No comments:

Post a Comment