Loading ...

How to do case sensitive search in SQL

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  data access   » How to do case sensitive search in SQL

How to do case sensitive search in SQL

Posts under the topic: How to do case sensitive search in SQL

Posted: 3/28/2010

Lurker 50  points  Lurker
  • Joined on: 3/28/2010
  • Posts: 10

In SQL usually when I search something the search is case insensitive like if I search for George it will give these results 

George

george

geORGE

etc..

 

Now I need to put a search which is case sensitive search like for me in the above example it should give only the "George" as output.

 


Posted: 4/5/2010

Starter 710  points  Starter
  • Joined on: 11/11/2009
  • Posts: 34
  Answered

Posted: 4/19/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

DECLARE @TestTable TABLE ([Character] CHAR(1))

INSERT @TestTable VALUES('a') 
INSERT @TestTable VALUES('A')
INSERT @TestTable VALUES('b')
INSERT @TestTable VALUES('B')
INSERT @TestTable VALUES('c')
INSERT @TestTable VALUES('C')
INSERT @TestTable VALUES('d')
INSERT @TestTable VALUES('D')


DECLARE @TargetTable TABLE 
([TargetColumn] VARCHAR(20) COLLATE Latin1_General_CS_AS)

INSERT INTO @TargetTable
SELECT t1.[Character] + t2.[Character] + t3.[Character] 
FROM @TestTable t1
CROSS JOIN @TestTable t2
CROSS JOIN @TestTable t3



--NOW TEST 

SELECT [TargetColumn] FROM @TargetTable
WHERE TargetColumn LIKE ('AA%')
--OUTPUT
--AAa
--AAA
--AAb
--AAB
--AAc
--AAC
--AAd
--AAD







SELECT [TargetColumn] FROM @TargetTable
WHERE TargetColumn LIKE ('Aa%')
--OUTPUT
--Aaa
--AaA
--Aab
--AaB
--Aac
--AaC
--Aad
--AaD



SELECT [TargetColumn] FROM @TargetTable
WHERE TargetColumn='AaA'
--OUTPUT
--AaA

 

The heart is at COLLATE Latin1_General_CS_AS


tags sql
Page 1 of 1 (3 items)