Solved

Write to VARCHAR(MAX)

Posted on 2011-03-23
15
855 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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