Skip to main content
Solved

Case Sensitive SQL Comparisons

  • December 3, 2020
  • 2 replies
  • 108 views

Does anyone know a good method for checking case sensitive user input using the control forms?

 

I would like to use Control forms to validate whether a user has entered at least one uppercase and one lowercase character, this is for validating password requirements. 

 

By default SQL is case-insensitive. A common workaround is using: 

COLLATE SQL_Latin1_General_CP1_CS_AS 

 

I have tried to implement this but it does not seem to be working. Am I doing something wrong? Or is there another method available for implementing case sensitive comparisons? 

My implementation

 

Best answer by Mark Jongeling

Hi Emiel,

After checking myself, I notice the collation you use does not work for this purpose. I tried this:

Both if statements should be False

For another purpose, cleaning table names, I have used the ASCII() function. It could also work for this. I rebuilded it a little for you. You can them make the changes needed to return the correct error(s). 

1declare @password varchar(256) = 'password'
2select @password = trim(@password)
3
4declare @length int = len(@password)
5 , @counter int = 1
6 , @correct int = 1
7 , @ascii_code int
8
9
10--Go through the password, one by one character
11while @counter <= @length
12 and @correct = 1
13begin
14 --Make a ASCII code of the string
15 select @ascii_code = ascii(substring(@password, @counter, 1))
16
17 --What is allowed
18 if @ascii_code between 48 and 57 or --Numbers
19 @ascii_code between 65 and 90 or --Uppercase letters
20 @ascii_code between 97 and 122 or --Lowercase letters
21 @ascii_code = 95 --Underscore
22 begin
23 --Up the counter
24 select @counter = @counter + 1
25 end
26 else
27 begin
28 select @correct = 0 --Password incorrect
29 end
30
31end
32
33if @correct = 1
34begin
35 select 'Correct'
36end
37else
38begin
39 select 'Incorrect'
40end
41

Hope this can help!

Kind regards,
Mark Jongeling

View original
Did this topic help you find an answer to your question?
This topic has been closed for replies.

2 replies

Mark Jongeling
Administrator
Forum|alt.badge.img+23
  • Administrator
  • 4034 replies
  • Answer
  • December 3, 2020

Hi Emiel,

After checking myself, I notice the collation you use does not work for this purpose. I tried this:

Both if statements should be False

For another purpose, cleaning table names, I have used the ASCII() function. It could also work for this. I rebuilded it a little for you. You can them make the changes needed to return the correct error(s). 

1declare @password varchar(256) = 'password'
2select @password = trim(@password)
3
4declare @length int = len(@password)
5 , @counter int = 1
6 , @correct int = 1
7 , @ascii_code int
8
9
10--Go through the password, one by one character
11while @counter <= @length
12 and @correct = 1
13begin
14 --Make a ASCII code of the string
15 select @ascii_code = ascii(substring(@password, @counter, 1))
16
17 --What is allowed
18 if @ascii_code between 48 and 57 or --Numbers
19 @ascii_code between 65 and 90 or --Uppercase letters
20 @ascii_code between 97 and 122 or --Lowercase letters
21 @ascii_code = 95 --Underscore
22 begin
23 --Up the counter
24 select @counter = @counter + 1
25 end
26 else
27 begin
28 select @correct = 0 --Password incorrect
29 end
30
31end
32
33if @correct = 1
34begin
35 select 'Correct'
36end
37else
38begin
39 select 'Incorrect'
40end
41

Hope this can help!

Kind regards,
Mark Jongeling


Forum|alt.badge.img+4

Hello Emiel,

To add to Mark’s answer, the reason it doesn’t work is because of the [A-Z] range in your LIKE comparison. These ranges appear to always be case-insensitive, but it does work if you write out the letters of the range.

Try out the following two queries and see how their results differ:

1select 1
2where 'abc' COLLATE SQL_Latin1_General_CP1_CS_AS like '%[A-Z]%'
3
4select 1
5where 'abc' COLLATE SQL_Latin1_General_CP1_CS_AS like '%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]%'

It's important to note that the _AS part of this collation indicates that it is accent-sensitive. This means that é will not match e and therefore a password like ÀàààÉééé will not match either of your LIKE-comparisons, causing the password to be rejected. You might want to consider the Latin1_General_CS_AI collation instead.

Hope this helps.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings