Check Constraints

  • Hello. I am a student and am trying to complete a homework assignment but can't figure it out. I am supposed to add a check constraint to one of my database tables that requires the Password field to be at least 8 characters. I have tried everything that was taught to me and can not come up with the solution. Can someone please point me in the right direction?

  • what have you tried and what is wrong with that you've tried?

  • there are two functions you can use to check the length of a string :

    LEN(SomeField)

    --or

    DATALENGTH(SomeField)

    both return integer values. I believe that your check constraint would have to use either of those functions to accomplish your assignment.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Use Len(Ltrim(Rtrim(Column_Name))) in your Check Constraint

    -RP
  • Rups (6/2/2010)


    Use Len(Ltrim(Rtrim(Column_Name))) in your Check Constraint

    This is a good solution, but rtrim is not needed. Len function ignores the trailing (but not leading) spaces. In other words len('hello ') is equal to 5, and therefore

    len(ltrim(Column_name)) > 8

    should do the the trick as well. For this solution, Len is also safer than datalength for 2 reasons:

    If the passwords are implemented as nvarchar then datalength returns twice the number as len

    Datalength does not ingore trailing spaces.

    Oleg

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply