Solved

Write to VARCHAR(MAX)

Posted on 2011-03-23
15
845 Views
Last Modified: 2012-05-11
Ok, this is probably really simple but.... how the hell do I write more than 8000 characters to a VARCHAR(MAX) in Managament Studio on SQL 2008?

If I use REPLICATE to write an arbitary string of 10000 characters and it's fine. If i try and write any actual data it trims it to 8000 characters.
0
Comment
Question by:Carl Tawn
15 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35200935
Varchar is limited. Use Text instead of the Varchar.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 35200969
How do you know it trims them?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 35200981
this works fine for me

declare @x varchar(max)
set @x = replicate('x', 8000)
set @x = @x + replicate('y', 8000)
print right(@x, 50)
print len(@x)
0
 
LVL 52

Author Comment

by:Carl Tawn
ID: 35200990
Because using SELECT DATALENGTH([MyColumn]) returns 8000, and only the first 8000 characters are being pulled back to my app.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 35201002
do you get all y's for the first line and 16000 for the 2nd above?
datalength works fine for me too.
0
 
LVL 52

Author Comment

by:Carl Tawn
ID: 35201077
Yes. As I said in my original question, writing to the column using REPLICATE works fine, it's just writing any actual real data that is being truncated.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 35201159
Can you show me some of the code you're using where the data gets trimmed?

and, do not use text...it will go away some day.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 52

Author Comment

by:Carl Tawn
ID: 35201191
Basically:
UPDATE [SomeTable] SET [SomeColumn] = 'some string with more than 8000 characters' WHERE [somecolumn] = 'some value'

Open in new window

The column is defined as VARCHAR(MAX)
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 35201242
this works fine for me.  Maybe your string is getting cutoff somehow before it hits the update statement?

create table #t(fld1 varchar(max))
declare @x varchar(max)
set @x = replicate('x', 8000)
set @x = @x + replicate('y', 8000)
print right(@x, 50)
print datalength(@x)
insert into #t
select @x


select right(fld1, 50) from #t

select datalength(fld1) from #t

update #t
set fld1 = fld1 + replicate('z', 8000)


select right(fld1, 50) from #t

select datalength(fld1) from #t
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35201366
Please verify that all variables / parameters used to hold your varchar(max) value are defined as varchar(max).
0
 
LVL 52

Author Comment

by:Carl Tawn
ID: 35201563
OK, after some experimenting, it appears to be an issue with the fact that I am embedding CHAR(13) and CHAR(9) into the data i am trying to write. So, how do I write to a VARCHAR(MAX) including those two characters?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 35201587
hmmm...that is interesting.  can you test to see if it works with a nvarchar(max) field?
0
 
LVL 52

Author Comment

by:Carl Tawn
ID: 35201590
Actually scratch that. Instead of:
'some text' + CHAR(13) + CHAR(9) + 'blah blah'

Open in new window

I had to use:
'some text' + CAST(CHAR(13) + CHAR(9) AS VARCHAR(MAX)) + 'blah blah'

Open in new window

Odd that it can't manage to convert them itself though.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 35201622
yeah, Im not surprised though.  Some of the ways it handles character values are strange.
0
 
LVL 52

Author Closing Comment

by:Carl Tawn
ID: 35201719
Not exactly a solution, but a very useful sanity check :)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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