简体   繁体   中英

Case sensitive record in SQL Server

I use SQL Server 2012. The database collation is 'CI' (Case Insensitive). When selecting from aspnet_users, the usernames is case insensitive as espected. But, some single records in the table still seems to be case sensitive, and I don't understand why.

Expample: I have a record with the username "MariaA".

This select statement returns the record correct:

SELECT [UserName] FROM [aspnet_Users] where UserName = 'MariaA'

This select statement also returns the record correct:

SELECT [UserName] FROM [aspnet_Users] where UserName = 'mariaA'

if I run this statement, the user is NOT returned:

SELECT [UserName] FROM [aspnet_Users] where UserName = 'Mariaa'

if I run this statement, the user is NOT returned:

SELECT [UserName] FROM [aspnet_Users] where UserName = 'mariaa'

Question: It seems like the upper case character 'A' is case sensitive. All other characters can be case insensitive. This problem also occur with other userprofiles, when the username contains the uppercase 'A' it cannot be written in lowercase 'a'.

Why is this happening, and how can it be solved?

use COLLATE SQL_Latin1_General_CP1_CS_AS in where clause

WITH cte AS
(

    select 'MariaA' as username
) select * from cte where username='mariaA' COLLATE SQL_Latin1_General_CP1_CS_AS

By default it is case insensitive

so the below query will return a row where it will match

WITH cte AS
(

    select 'MariaA' as username
) select * from cte where username='MariaA' COLLATE SQL_Latin1_General_CP1_CS_AS

In Danish collation aa is a single character, while aA is interpreted as two characters (regardless of case sensitivity of the collation).

If it is practical, you can do the comparison in upper case:

WITH cte AS
(

    select 'MariaA' as username 
) 

SELECT * from cte where UPPER(username)=UPPER('Mariaa') COLLATE Danish_Norwegian_CI_AS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM