• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2456
  • Last Modified:

Can I set a minimum character string length on varchar?

Hello Experts:

I want to know, if I can enforce a  minimum character string length on varchar column?
I want all values to be 8 characters or greater.

Thanks in advance!
Keith

0
glo-dba
Asked:
glo-dba
1 Solution
 
tiagosalgadoCommented:
If you have an application who insert data at that column, you can enforce it at that level.
Directly on SSMS, i think is not possible.
0
 
Patrick MatthewsCommented:
Hello glo-dba,

You can use a check constraint to enforce a minimum length.  This silly example creates a one-column
table, and enforces a minimum length of 8 characters:

CREATE TABLE foo (hee varchar(100), CONSTRAINT hee_min_len CHECK (LEN(hee) >= 8))

Regards,

Patrick
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
As tiagosalgado said, you can handle it in the application.
Or
You can achieve that in the table using triggers.
Use triggers to check for the minimum length of that varchar column. if it's length is less than 8 and do some manipulations and save it.

Hope this solves your requirement.
0
 
glo-dbaAuthor Commented:
Perfect answer!

This was the correct answer. I do not have control over what the development team write into their application. The field in question is meant to be a password field. I cannot enforce a proper security schema but at least now I can enforce our password length policy.

Thanks,
Keith
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now