Can I set a  minimum character string length on varchar?

Posted on 2009-02-21
Last Modified: 2012-05-06
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!

Question by:glo-dba
    LVL 8

    Expert Comment

    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.
    LVL 92

    Accepted Solution

    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))


    LVL 57

    Expert Comment

    by:Raja Jegan R
    As tiagosalgado said, you can handle it in the application.
    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.

    Author Closing Comment

    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.


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now