Solved

Write to VARCHAR(MAX)

Posted on 2011-03-23
15
838 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

12 Experts available now in Live!

Get 1:1 Help Now