Question

I'm looking to create three separate check constraints for my SQL Server database to ensure that a password field constrains a minimum of one number, one uppercase character and one special character.

I consider the best way to approach is by creating separate check constrains for example I have created the following constraint to ensure that password length is a minimum of 8

(len([password]) <= (8))

Could any one suggest a way to establish the required validation.

Thanks

Was it helpful?

Solution

You can do this with one constraint, something like:

check password like '%[0-9]%' and password like '%[A-Z]%' and password like '%[!@#$%a^&*()-_+=.,;:'"`~]%' and len(password) >= 8

Just a note about the upper case comparison: this requires that the collation being used be case sensitive. You may need to specify an explicit COLLATE in the comparison to be sure that it is case sensitive.

OTHER TIPS

I did not know answer for your question directly, but after some research i found this methods:

1) Check if given string contains uppercase character

SELECT CASE WHEN BINARY_CHECKSUM([password]) = BINARY_CHECKSUM(LOWER([password])) THEN 0 ELSE 1 END AS DoesContainUpperCase
GO

source: http://www.sqlservercentral.com/Forums/Topic800271-338-1.aspx

2) Check if given string contains at least one special character

We can make simply statement

IF ([password] LIKE '%[^a-zA-Z0-9]%')

source: How to detect if a string contains special characters?

I hope my answer helped you. Kind regards,

Rafal

I had to remove the 'a' in the collection of special characters and added the collate mentioned above. Thanks for your help!

([password] like '%[0-9]%' AND [password] like '%[A-Z]%' collate Latin1_General_BIN2 AND [password] like '%[!@#$%^&*()-_+=.,;:~]%' AND len([password])>=(8))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top