About Me

My photo
જય વાળીનાથ
T-SQL is a basic of structure query language. So I always want to learn T-SQL in different way to get best performance in query. And its my passion.

Saturday, April 25, 2009

case sensitive searches....Script in T-SQL

When we are using where clause for conditional search sql using case insensitive and produce result.

But some time we need to search case sensitive then i show you some example for it...

First create one table.
CREATE TABLE [dbo].[checkName](
[Id] [int] IDENTITY(1,1) PRIMARY KEY,
[Cname] [varchar](100)

and execute above script.

then insert some records in above table.
insert into checkName (Cname) values('Anil')
insert into checkName (Cname) values('aNil')
insert into checkName (Cname) values('anIl')
insert into checkName (Cname) values('aniL')
insert into checkName (Cname) values('AnIl')

execute above script.

then execute below script for finding value of 'aniL'

select *
from checkName
where BINARY_CHECKSUM(Cname) = BINARY_CHECKSUM('aniL')
-------------------------------------------------------------------------------------------
select *
from checkName
where Cname = 'aniL' COLLATE SQL_Latin1_General_CP1_CS_AS
-------------------------------------------------------------------------------------------
select *
from checkName
where CAST(Cname AS varbinary(100)) = CAST('aniL' AS varbinary(100))
-------------------------------------------------------------------------------------------

Output will be....

Id Cname
4 aniL




No comments: