May 31, 2010 at 8:17 am
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?
May 31, 2010 at 8:25 am
what have you tried and what is wrong with that you've tried?
May 31, 2010 at 8:26 am
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
June 2, 2010 at 10:07 am
Use Len(Ltrim(Rtrim(Column_Name))) in your Check Constraint
June 2, 2010 at 3:34 pm
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