SQL string sensitive search

declare @reason varchar(4000)

set @reason = 'Test, Reason'

select *
from Transactions t
where t.UserReason in (select item from dbo.CreateStringTable(@reason))

Open in new window


I have a stored procedure which accepts @reason as an incoming string parameter, but for the purposes of this post, I declred @reason manually, and assigned it some test values. In the stored procedure, we use a function "CreateStringTable", which accepts a string of comma separated values, and returns them as a column in a table.

To avoid confusion, the query above can be seen as:
select *
from Transactions t
where t.UserReason in (select columnA from TableA)

Open in new window



How do I do a case-sensitive search of this? In other words, if columnA only contains the word 'Test', and t.UserReason contains only the word 'test', no records should be returned.
LVL 8
pzozulkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
you can use a Case-Sensitive collation:
Select * from a_table where attribute = 'k' COLLATE Latin1_General_CS_AS 

Open in new window

pzozulkaAuthor Commented:
I get an error message: Incorrect syntax near the keyword 'COLLATE'. Query below:
Select * from a_table 
where attribute in (select columnA from TableA) COLLATE Latin1_General_CS_AS 

Open in new window

Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
You can use case sensitive search using collate in your statement like.

select * from Transactions t where t.UserReason  COLLATE Latin1_General_CS_AS in (select columnA from TableA)


or

if you don't want to right every time this in your query you can alter your column of table to be case sensetive.

alter table Transactions
UserReason <datatypeof column>
COLLATE Latin1_General_CS_AS

if data type id varchar (50) and column allows null then

alter table Transactions
UserReason varchar(50) null
COLLATE Latin1_General_CS_AS



but it will do every time case sensitive compare.

hope it will help you

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pzozulkaAuthor Commented:
Thanks Arif, that worked out great.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.