Search code examples
sqlsql-servercheck-constraint

Check Constraint- Check password contains atleast one number/special Character/uppercase


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


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.