Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Write to VARCHAR(MAX)

Posted on 2011-03-23
15
Medium Priority
?
870 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

782 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