SQL SERVER – 2005 Constraint on VARCHAR(MAX) Field To Limit It Certain Length

One of the Jr. DBA at in my Team Member asked me question the other day when he was replacing TEXT field with VARCHAR(MAX) : How can I limit the VARCHAR(MAX) field with maximum length of 12500 characters only. His Question was valid as our application was allowing 12500 characters.

Traditionally thinking we only create the field as long as we need. SQL Server 2005 does support VARCHAR(MAX) but does not support VARCHAR(12500). If we try to create database field with VARCHAR(12500) it gives following error.

Server: Msg 131, Level 15, State 3, Line 1
The size (12500) given to the type ‘varchar’ exceeds the maximum allowed for any data type (8000).

The solution we came up was to put constraint on the VARCHAR(MAX). SQL SERVER 2005 supports VARCHAR(MAX) and Constraint.

CREATE TABLE [dbo].[MyTestTable] ( [VarChar12500] VARCHAR(MAX) )
GO
ALTER TABLE [dbo].[MyTestTable] ADD CONSTRAINT [MaxLengthConstraint] CHECK (DATALENGTH([VarChar12500]) <= 12500)
GO

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Constraint and Keys, SQL Scripts, SQL String
Previous Post
SQL SERVER – Retrieve Information of SQL Server Agent Jobs
Next Post
SQLAuthority News – Dedicated Search Engine for SQLAuthority – Search SQL Solutions

Related Posts

50 Comments. Leave new

  • Hi,

    I was trying to use this query and I’m getting an error stating incorrect syntax near MAX. Please help me out with this as I’m trying to pass some text into the database with more than 10000 chars.

    Thanks and Regards,

    Turya

    Reply
    • It is becuase you are using version prior to 2005. Varchar(max) is supported from version 2005

      Reply
  • how can i change the table field order ???
    example for I have one table name XYZ and field are Code,Name,Address,Pin how I change the sequence like Name,Code,Address,Pin …. Please Help Me

    Shanku

    Reply
  • Hello Sir ,
    how can change Primary key from SQL prompt .. Please help me…

    Reply
  • im having problem in sending query online to create a table with varchar(max)
    they say that the server is sql 2k5 and it seems like its 2005.

    but i have replaced max with 300.. cuz i dont really need too much details but what was wrong there?

    Reply
  • Sir,
    How I can wrote syntax to find the “Maximum of 10 values from a table” like Salary Column using SQL Server 2005

    Reply
  • hello sir,
    i want to use MAX function in insert query, how can i do that?

    insert into xyz(id,name) values(max(id),’nirav dave’)

    i want to use max(id) in insert query coz i don’t want to fire special query to find max number of id,

    plz. help me.

    Reply
  • Hi !
    Can you help me please ?

    I had to change our database (not empty) collation from French_CI_AS to SQL_Latin1_General_CP850_CI_AI.

    : Database\Properties\Options\Collation. Change it !

    After doing this, i noticed that only data on fields with “varchar(max) ” type were truncated.
    Like, even if i put 100 characters by the interface, no problem but only 12 characters are really saved in the database. Troncation happens only on these types of fiels et always the same number of characters 12

    Can anyone knows why and how avoid this ?

    Thank you very much and sorry, i do not speak english very well :(

    Reply
  • Hi,

    I was trying to use this query and I’m getting an error stating incorrect syntax near MAX. Please help me out with this as I’m trying to pass some text into the database with more than 12000 chars.

    please send right qeury
    on my email id i need it very ugently

    Thanks and Regards,

    ajay

    Reply
  • Even I declare

    local Variable with VARCHAR(MAX)

    its Given only 8000 As Output..

    please Suggest.

    Reply
  • Hi All,

    i am in doubt whether the constraints are stored in sys_objects or information_schema. table_constraints.

    please help me out.

    Reply
  • Hello Sir
    my problem is: i enter the text in sql 2005. but some tile text cross the 8000 char which is max one.

    so i got error of data truncation

    how i solve this problem

    Reply
  • Hello,

    I need to declare a variable with Varchar of size 12000 in stored procedure. But in SQL Server it gives me 8000 only as the extreme limit. So how can I increase that upto 12000.

    help me please.

    Regards
    ~ riti

    Reply
  • What are the limitation of varchar(MAX) in sql server 2005 ???

    Reply
  • Sir,

    I want to know the Maximum Sizes each of these Data Type can occupy(in terms of bytes)
    VARCHAR(MAX)
    NVARCHAR(MAX)
    NVARBINARY(MAX)
    IMAGE

    I am confused!!! can you please help me out ?

    Thanks

    Reply
  • Karthik> up to 2 GB. You can find this info in MSDN, stupid.

    Reply
  • Hi. How many fields we can insert a table on sql 2005?

    Reply
  • Hello Sir,

    How the SQL server work around MAX(VARCHAR).

    Yogesh.

    Reply
  • Amrut D kanthavadiya
    May 7, 2010 6:59 pm

    i want to Store 12000 character string within single cell of column in table

    Error : The size (12000) given to the type ‘varchar’ exceeds the maximum allowed for any data type (8000)

    ———————————-

    Give me SQL server 2008 Query Only

    i am Builting Binary Tree for MLM .

    Thanks in Advance.

    not give me “GAPPA” answer

    amrut kanthavadiya

    Reply
  • Hi,

    I am using MSSQL-2008

    In my one of procedure im using dynamic Query and it is getting save in nvarchar(MAX)

    Here problem is that –

    My dynamic query is exceeding the length max limit and my dynamic query is not firing

    Is there any way-out to save LARGE queries in nvarchar

    Reply
  • Hi, I am using

    DECLARE @Query AS VARCHAR(MAX)

    for maximum character. But Sp is going an error
    Msg 170, Level 15, State 1,
    Line 18: Incorrect syntax near ‘VARCHAR’.

    Please tell me what should i do for take maximum character.

    Reply
    • Which version of SQL Server are you using?

      Reply
      • SQL Server 2005

      • My SP is something like:

        ALTER PROC [dbo].[spEditorReport_Test]
        ( @PJID VARCHAR(20),
        @Pstartdate VARCHAR(20),
        @Penddate VARCHAR(20),
        @PEditorId VARCHAR(20),
        @IsDateAssigned BIT

        )
        AS
        BEGIN
        SET nocount ON
        DECLARE @jid VARCHAR(20),
        @StartDate VARCHAR(20),
        @endDate VARCHAR(20),
        @DBName VARCHAR(20),
        @Query VARCHAR(MAX)

        SET @DBName = ‘nature’
        SET @Query = ‘DECLARE @jid AS VARCHAR(20),
        @StartDate AS VARCHAR(50),
        @endDate AS VARCHAR(20),
        @DBName As NVARCHAR(20),

        ………………………………..
        ………………………………..
        (Having Big Length)
        ………………………………..
        ………………………………..

        print (@Query)
        exec (@Query)
        END

        Output: Only printing 8000 characters like –

        DECLARE @jid AS VARCHAR(20),
        @StartDate AS VARCHAR(20),
        @DBName As NVARCHAR(20),
        @PEditorId AS NVARCHAR(20),
        @IsDateAssigned BIT

        ……………. (Till 8000 char, rest it leaves)

        Please help me how can i print all characters???
        I have tired to try a lot to resolve this error??????

Leave a Reply