Skip Navigation Links
Home
Functions
Tips & Tricks
SQL Server 2005
SQL Server 2008
SQL Server 2012
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : How to Limit a VARCHAR Column to 10,000 Characters
Welcome to SQL Server Helper!!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

 
SQL Server Tip of the Day - January 28, 2012
How to Limit a VARCHAR Column to 10,000 Characters

VARCHAR(MAX) data type was introduced in SQL Server 2005.  Prior to having VARCHAR(MAX), in SQL Server 2000, the maximum length that can be specified for the VARCHAR data type is 8000.  The maximum length for a column that is defined as VARCHAR(MAX) data type is 2,147,483,645 characters.

Assuming you need to define a VARCHAR column that has a maximum of 10,000 characters.  Defining the column as VARCHAR(10000) will generate the following error message:

Msg 131, Level 15, State 3, Line 1
The size (10000) given to the type 'varchar' exceeds the maximum allowed for any data type (8000).

To avoid this error as well as to accomplish the requirement of having a maximum of 10,000 characters for a column, what you can do is to define the column as VARCHAR(MAX).  Then to make sure that a maximum of 10,000 characters only are stored in this column, a CHECK constraint needs to be created against the column that checks the length of the value being stored in the column.

To illustrate, assuming you have a table called [dbo].[Company] that contains a column called [CompanyProfile] defined as VARCHAR(MAX).  To limit the length of this column to 10,000 characters, the following constraint needs to be created:

ALTER TABLE [dbo].[Company]
    ADD CONSTRAINT [MaxLength10000]
    CHECK (DATALENGTH([CompanyProfile]) <= 10000)
 
SQL Server 2012
 
SQL Server 2008
What's New in SQL Server 2008

A collection of articles related to what's new with SQL Server 2008:

SQL Server 2008 Articles

A collection of articles related to SQL Server 2008:

 
User-Defined Functions
Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions
A collection of useful table-valued user-defined functions that can be used to join with other tables.
SQL Server Built-in Functions
A reference to all built-in functions available within SQL Server grouped into categories.
 
Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

 
Frequently Asked Questions
SQL Server

A collection of frequently asked questions about SQL Server grouped by categories:

SQL Server 2005

A collection of frequently asked questions about SQL Server 2005 grouped by categories: