To find max length of datatype

  • Is there any way to find out the max number of characters the datatypes varchar(max), nvarchar(max) can occupy?

    I had a variable of varchar(max) and another of nvarchar(max) and both seemed to occupy max of 65535 characters.

    Which datatype occupies the largest number of characters?

  • varchar(max), nvarchar(max), varbinary(max), text, image are large value data types. They can store upto 2^31-1 bytes of data (i.e. 2,147,483,647 characters) and with the unicode version you can store upto 2^30-1 (i.e. 1,073,741,823 characters)

    --Ramesh


  • The maximum size for a VARCHAR(MAX) is 2^31 - 1 bytes / characters.

    When i run the below query and check

    create table #tmp_xml (xmlrow varchar(max))

    insert into #tmp_xml (xmlrow)

    select replicate(cast('*' as varchar(max)), 20000000)

    select len(xmlrow) from #tmp_xml

    The length is correctly output as 20000000.

    But the select displays a max of 65535 characters followed by '...'

    xmlrow

    ----------------

    *************...

    So I guess its not displaying all the characters via select statement.

    Is there any way that i can retrieve all the characters?

  • Check the attached screenshot.

    Max

  • Excellent..Thanks!! ๐Ÿ™‚

    So, I can't change the value of Non-xml data here? I tried but it didn't. Is there a way?

    But it gave me an idea. I typecast my result to xml datatype and it works fine!! Thanks!!

  • No, sorry, should have mentioned that the screenshot depicts the non-xml length per column as at maximum already.

    Max

  • If Non-xml datatypes returns max of 65535 characters, then how can we get all the characters that varchar(max) is storing. There must be some way to retrieve the data..

  • ...Till now, I haven't found an easy way to get all in the pane, but there is a work-around, that needs to split the text in multiple columns, i.e.

    DECLARE @LongText VARCHAR(MAX)

    SELECT@LongText = replicate(cast('*' as varchar(max)), 70000)

    SELECTSUBSTRING( @LongText, 1, 60000 ) AS LongText1,

    SUBSTRING( @LongText, 60001, 120000 ) AS LongText2

    --Ramesh


  • Well, this solved my need today. I casted it to xml datatype.

    create table #tmp_xml (xmlrow xml)

    insert into #tmp_xml (xmlrow)

    select cast(replicate(cast('*' as varchar(max)), 20000000) as xml)

    select * from #tmp_xml

  • Looks like Ramesh has this sussed, but what do you get for this, as a matter of interest (I don't have 2k5 to play with right now)...

    DECLARE @MyEnormousBelly VARCHAR(max)

    SET @MyEnormousBelly = replicate(cast('*' AS varchar(max)), 20000000)

    CREATE TABLE #tmp_xml (xmlrow varchar(max))

    INSERT INTO #tmp_xml (xmlrow)

    SELECT @MyEnormousBelly

    SELECT @MyEnormousBelly = xmlrow

    FROM #tmp_xml

    SELECT LEN(@MyEnormousBelly), DATALENGTH(@MyEnormousBelly)

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Chris Morris (2/19/2009)


    Looks like Ramesh has this sussed, but what do you get for this, as a matter of interest (I don't have 2k5 to play with right now)...

    DECLARE @MyEnormousBelly VARCHAR(max)

    SET @MyEnormousBelly = replicate(cast('*' AS varchar(max)), 20000000)

    CREATE TABLE #tmp_xml (xmlrow varchar(max))

    INSERT INTO #tmp_xml (xmlrow)

    SELECT @MyEnormousBelly

    SELECT @MyEnormousBelly = xmlrow

    FROM #tmp_xml

    SELECT LEN(@MyEnormousBelly), DATALENGTH(@MyEnormousBelly)

    ....A long break, a cup of coffee and a resultset with values 2000000020000000:w00t:

    --Ramesh


Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply