Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Sensitive String Comparison

I need to compare two strings in a If-Else block in a stored procedure such as

If(@USERNAME='rajat')
begin
select * from table
end
else
begin
select * from table1
end

but the problem with the above approach is '=' comparison is case insensitive.I need a approach where the comparison is case sensitive

like image 273
rampuriyaaa Avatar asked Nov 28 '13 12:11

rampuriyaaa


2 Answers

SQL Server has case sensitivity at the server, database, and column level. This is part of the collation properties. So in your example, it's likely that one or more of these settings has been set to case-insensitive.

-- Check server collation
SELECT SERVERPROPERTY('COLLATION')

-- Check database collation.
SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

-- Check column collation
select table_name, column_name, collation_name
from information_schema.columns
where table_name = @table_name

Something like this SQL might work for you:

If (@USERNAME='rajat' COLLATE Latin1_General_CS_AS)
.....
like image 65
HTTP 410 Avatar answered Sep 20 '22 13:09

HTTP 410


If(@USERNAME='rajat' COLLATE Latin1_General_CS_AS )
begin
select * from table
end
else
begin
select * from table1
end
like image 43
Mikhail Timofeev Avatar answered Sep 20 '22 13:09

Mikhail Timofeev