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 (http://blog.SQLAuthority.com)












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
It is becuase you are using version prior to 2005. Varchar(max) is supported from version 2005
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
Hi Shanku,
You can not do that. You will have to create new table with your desired column order and insert values from old table to new table. Drop the old table and rename the new table with the name of old table.
However, the order of column does not matter to database.
Regards,
Pinal Dave(SQLAuthority.com)
Hello Sir ,
how can change Primary key from SQL prompt .. Please help me…
Generate the script of the table and modify it accordingly
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?
Check the compatibility of the database. If it is less than 90, change it to 90
Sir,
How I can wrote syntax to find the “Maximum of 10 values from a table” like Salary Column using SQL Server 2005
select top 10 salary from table
order by salary desc
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.
There is no such way to do it directly
insert into xyz(id,name) select max(id),’nirav dave’ from xyz
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 :(
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
set the compatibility of the database to 90 and try
Even I declare
local Variable with VARCHAR(MAX)
its Given only 8000 As Output..
please Suggest.
What does this give?
select len(@var)
Hi All,
i am in doubt whether the constraints are stored in sys_objects or information_schema. table_constraints.
please help me out.
sys.sysconstraints or information_schema. table_constraints.
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
You need to set the compatibility of the database to 90
[...] SQL SERVER – 2005 Constraint on VARCHAR(MAX) Field To Limit It Certain Length [...]
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
use varchar(max)
What are the limitation of varchar(MAX) in sql server 2005 ???
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
Karthik> up to 2 GB. You can find this info in MSDN, stupid.
Hi. How many fields we can insert a table on sql 2005?
Read about Maximum Capacity Specifications in SQL Server help file
Hello Sir,
How the SQL server work around MAX(VARCHAR).
Yogesh.
Your question is not very clear?
Can you give more informations?
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
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
What type of query that you are generating?
Can you give us an example?
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.
Which version of SQL Server are you using?
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??????
Hi,
I’m Constructing a Dynamic Query in a Stored Procedure with Variable as
DECLARE @Query VARCHAR(MAX)
But when i try to print the constructed Query (Print @Query)
it shows only around or less than 8000 chars, rest got truncated.
Please provide me some solution.
thanks,
Print statement will display first 8000 characters only
Hi,
I really need some help~
I am trying to use a batch file with fmt file to insert data into a table which I had declared as varchar (max), but somehow I keep encounter some problem as below:
SQLState = HY000, NativeError = 0
Error = [Microsoft][SQL Native Client]Invalid field size for datatype
My BCP statement:
bcp [Database].dbo.[table] in D:\20110815.ASC -fD:\fedline.fmt -F2 -Uuid -Ppwd -Ssvr -e ERROR.LOG
this is my fmt file.
7.0
1
1 SQLCHAR 0 14000 “\r\n” 1 data_str
I am using SQL Server 2005, when I tried to change the fmt file to version 9.0 which is same as SQL Server 2005, I get the following error:
SQLState = HY000, NativeError = 0
Error = [Microsoft][SQL Native Client]I/O error while reading BCP format file
Could you please help me out? Thanks.
Hi
I am using SQL server 2005; i created a store procedure to put query result into XML into String.
declare @returntext varchar(max)
set @ReturnText = CAST ((
select A, B, C, D from tableE for xml path , TYPE) AS VARCHAR(MAX))
Total Length from the result should be 20000 characters; but it only return 8000 characters.
Anything I can improve it and make it work.
Thanks
Great..!!!!! It worked for me… Thanks a lot my friend..
the problem lies with some result output setting of SQL Server Management Studio..i run such queries on 3rd party database client – Toad for Data Analysts, it all worked well and displayed the complete string that exceeded 15000..
[...] SQL Server Interview Questions and Answers ISBN: 1466405643 Page#85-87 Replace TEXT with VARCHAR(MAX) – Stop using TEXT, NTEXT, IMAGE Data Types Constraint on VARCHAR(MAX) Field To Limit It Certain Length [...]
hi i am new to sql server 2005 can we restrict the user login for certain table in sql
thanks for the valuable guidance,this article helped me a lot.
Can the ADD CONSTRAINT [MaxLengthConstraint]
CHECK (DATALENGTH([VarChar12500]) <= 12500)
be applied on a SQl Variable declared in a Storedprocedure?.
I am really new to sql – I need to look at a database and determine the number of occurances of names with only 4 characters and then sort them in order.
LEN and DATALENGTH keep telling me that they do not exist?
select [name],count(name) from tablename where len(name)=4 group by [name] order by name asc
how to set compatibility of the database ?