Solved

Write to VARCHAR(MAX)

Posted on 2011-03-23
15
853 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 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