SQL Server varchar(max) is not working properly

Viewed 379

In the following example, the txt column length is not correctly shown.

CREATE TABLE dbo.test
(
    Id int IDENTITY,
    txt varchar(max)
);

insert into test(txt) select REPLICATE('0',8000);
insert into test(txt) select REPLICATE('00',8000);

select id, len(txt) from test;

drop table test;

The result shown for both were 8000.

Could any body help?

1 Answers

If you want to insert something into a varchar(max) column - your inserting code needs to explicitly convert it to varchar(max) - try this:

insert into test(txt) select REPLICATE(cast('00' as varchar(max)), 8000);

That should give you a string of 16'000 characters length in the table.

Related