SQL SERVER – Puzzle and Answer – REPLICATE over 8000 Characters

It has been a long time since we have played a puzzle over this blog. This Monday, let us play a quick puzzle.

SQL Server have REPLICATE function which will replicate the string passed as many as times as the second parameter. For example execute following string.

SELECT 'Ha'+REPLICATE('ha',20)

The script above will return following result:

SQL SERVER - Puzzle and Answer - REPLICATE over 8000 Characters replicatepuz1
`

You can notice that it has returned a string ha about 20 times after first Ha.

Now let us run following script.

DECLARE @FirstString VARCHAR(MAX)
DECLARE @SecondString VARCHAR(MAX)
DECLARE @ThirdString VARCHAR(MAX)
SET @FirstString = REPLICATE('A',4000)
SELECT LEN(@FirstString) LenFirstString;
SET @SecondString = REPLICATE('B',8000)
SELECT LEN(@SecondString) LenSecondString;
SET @ThirdString = REPLICATE('C',11000)
SELECT LEN(@ThirdString) LenThirdString;

The script above will return following result:

SQL SERVER - Puzzle and Answer - REPLICATE over 8000 Characters replicatepuz2

Quiz 1:

Pay attention to the last resultant and you will notice that the length of the @ThirdString is only 8000. WHY?

Here is the second part of the Quiz.

Quiz 2:

What changes I should do in the REPLICATE function so it will give the appropriate result in the case of @ThirdString. In our case, it should return the value of 11000 and not 8000.

DB Optimizer

One last thing: Do not forget to download DB Optimizer XE3.5 Pro. It is my favorite tool for performance tuning.

Please leave a comment with your answer. I will publish the answer to this puzzle on coming Friday’s blog post and will mention the names of the all the valid users.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

SQL Function
Previous Post
SQL SERVER – Interesting Observation of CONCAT_NULL_YIELDS_NULL and CONCAT
Next Post
SQL SERVER – How to use xp_sscanf in Real World Scenario?

Related Posts

154 Comments. Leave new

  • The string_expression component of the REPLICATE function needs to be VARCHAR(MAX) or NVARCHAR(MAX) to enable the output string to exceed 8000 characters

    So for the last example use:

    DECLARE @ThirdString VARCHAR(MAX);
    DECLARE @ThirdSource VARCHAR(MAX);
    SET @ThirdSource=’C’;
    SET @ThirdString = REPLICATE(@ThirdSource,11000);
    SELECT LEN(@ThirdString) LenThirdString;

    or

    DECLARE @ThirdString VARCHAR(MAX);
    SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000);
    SELECT LEN(@ThirdString) LenThirdString;

    Reply
  • Answer
    Quiz 1 : Last result is 8000, because maximum length of varchar is 8000 (if varchar(max) is not specified). We passed only varchar value to replicate function,so it returns value as varchar(8000)

    Quiz 2: To get 11000 result, replace second last line by following line
    SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000)

    Reply
  • Arsen Barbakadze
    May 13, 2013 9:31 am

    Hi Pinal,
    I think there is a typo in Quiz 1, the length of the @ThirdString is 8000 but not 4000. I think it is because the size if varchar(max) is 8000 characters, so when we use it implicitly then it restricted to 8000 characters.
    To get the length greater than 8000 characters I used this statement:

    SET @ThirdString = REPLICATE(CAST(‘C’ AS varchar(max)),11000);

    Best regards,
    Arsen

    Reply
  • you need to force the data type to varchar(MAX) like so

    DECLARE @ThirdString VARCHAR(MAX)
    SET @ThirdString = REPLICATE(CAST(‘D’AS varchar(MAX)),11000)
    SELECT LEN(@ThirdString ) LenThirdString;

    Reply
  • Mahesh Kumar
    May 13, 2013 10:13 am

    Hi Pinal,

    Answer1 : — In this case SQL server is displaying varchar maximum length which is 8000.

    Answer2 : – To resolve this problem we need to use cast function and statement should be —
    SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000)

    Reply
  • Hi Pinal,

    Answer1 : — In this case SQL server is displaying varchar maximum length which is 8000.

    Answer2 : – To resolve this problem we need to use cast function and statement should be —
    SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000)

    Reply
  • DECLARE @FirstString VARCHAR(MAX)
    DECLARE @SecondString VARCHAR(MAX)
    DECLARE @ThirdString VARCHAR(MAX)
    SET @FirstString = REPLICATE(‘A’,4000)
    SELECT LEN(@FirstString) LenFirstString;
    SET @SecondString = REPLICATE(‘B’,8000)
    SELECT LEN(@SecondString) LenSecondString;
    SET @ThirdString = REPLICATE(CONVERT(VARCHAR(MAX),’C’),11000)
    SELECT LEN(@ThirdString) LenThirdString;

    Reply
  • I think 8000 character is the initial max limit for Replicate..to go beyond that we need explicit conversion..I hv just tried it and it worked for me..But please make me clear sir..
    Thanx

    Reply
  • @TihrdString is return 8000 due to the max value of varchar i.e. 2^31 -1

    Reply
  • HI,

    Answer 1 :-
    The REPLICATE function frequently uses long strings, and therefore is more likely to incur the 4000-character limit on expression length.

    Answer 2 :-
    REPLICATE function truncate after 8000 character. I came across this problem few months ago, when i was playing with varchar. If we want to show Correct character len, then we have to break it in two parts and explicitly convert it again in Varchar(max). Have a look on below code.

    DECLARE @FirstString VARCHAR(MAX)
    DECLARE @SecondString VARCHAR(MAX)
    DECLARE @ThirdString VARCHAR(MAX)
    SET @FirstString = REPLICATE(‘A’,4000)
    SELECT LEN(@FirstString) LenFirstString;
    SET @SecondString = REPLICATE(‘B’,8000)
    SELECT LEN(@SecondString) LenSecondString;

    —- INCORRET
    SET @ThirdString = REPLICATE(‘C’,11000)
    SELECT LEN(@ThirdString) LenThirdString;

    —- CORRECT
    SET @ThirdString = CAST(REPLICATE(‘C’,5000) as VARCHAR(max))+REPLICATE(‘C’,6000)
    SELECT LEN(@ThirdString) LenThirdString;

    Thanks
    Vinay Kumar

    Reply
  • Sourav Ghosh
    May 13, 2013 11:55 am

    Hi Pinal,

    First of all, I am a great fan of your articles and posts… You have really helped me a lot in gaining speed in SQL.

    Now the answer:

    First the Correct SQL to display as per requirement:

    DECLARE @FirstString VARCHAR(MAX)
    DECLARE @SecondString VARCHAR(MAX)
    DECLARE @ThirdString VARCHAR(MAX)
    SET @FirstString = REPLICATE(‘A’,4000)
    SELECT LEN(@FirstString) LenFirstString;
    SET @SecondString = REPLICATE(‘B’,8000)
    SELECT LEN(@SecondString) LenSecondString;

    SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000)

    SELECT LEN(@ThirdString) LenThirdString;

    Reason:

    Replicate has two parameters: The first one in the character expression and second one is the integer value…

    The second one is not of the concern here.

    But the Character expression is implicitly convertible to NVARCHAR or NTEXT and by default as we all know it defaults to 8000 characters. So, the output of the Replicate function can never be more than 8000 characters

    So in order to get something more than 8000, we will need to CAST the character expression into MAX (VARCHAR/NVARCHAR) and then perform the replicate on the Character. Now since the variable getting assigned to is also specified in MAX, so it will store 11000 characters and LEN will display the correct value.

    Reply
  • Shankar Prabhu
    May 13, 2013 11:59 am

    Quiz 1: Varchar max length is 8000
    Quiz 2:
    SET @ThirdString = REPLICATE(convert(varchar(MAX), ‘C’), 20000);
    SELECT LEN(@ThirdString) LenThirdString;

    Reply
  • DECLARE @String1 AS VARCHAR(3000)
    DECLARE @String2 AS VARCHAR(6000)
    SET @String1=REPLICATE(‘a’,3000)
    SET @String2=REPLICATE(‘b’,6000)
    SELECT LEN(@String1+@String2) AS Result_String

    –RESULT IS 8000

    –Now if we change datatype to VARCHAR(MAX)

    DECLARE @String1 AS VARCHAR(MAX)
    DECLARE @String2 AS VARCHAR(MAX)
    SET @String1=REPLICATE(‘a’,3000)
    SET @String2=REPLICATE(‘b’,6000)
    SELECT LEN(@String1+@String2) AS Result_String

    –RESULT IS 9000

    –I think whats happening in above query is Result_String is varchar(9000) which exceeds the limit which is VARCHAR(8000)..so to get correct results we need to cast or convert it to varchar(max)

    –in second query strings are of VARCHAR(MAX) type so the result will be of varchar(MAX) type and we get the right result..

    Reply
  • hello dave
    this script will replicate string over 8000 characters
    SELECT LEN(@SecondString) LenSecondString;
    SET @ThirdString = REPLICATE(cast(‘C’ as varchar(max)),11000)
    SELECT LEN(@ThirdString) LenThirdString;

    Reply
  • Sivakumar Vellingiri
    May 13, 2013 12:35 pm

    Pinal,

    My answers are as follows :

    Ans 1 :

    REPLICATE function will truncate the value at 8000 bytes. We need to explicitly convert to VARCHAR(MAX) for large string (Ref Ans 2).

    Ans 2 :

    SET @ThirdString = REPLICATE(CONVERT(VARCHAR(MAX),’C’),11000)
    SELECT LEN(@ThirdString) LenThirdString;

    Note : Please change @ThirdString value 8000 instead of 4000 in Quiz 1.

    Reply
  • Hemant Gupta
    May 13, 2013 1:07 pm

    Is this because Len() can only compute for varchar(8000) and for larger we need to use DATALENGTH?

    Reply
  • 1) REPLICATE support 8000 at a time
    2) 11000 len can be achieve by following way
    SET @ThirdString = REPLICATE(‘C’,8000)
    SET @ThirdString = @ThirdString + REPLICATE(‘C’,3000)
    SELECT LEN(@ThirdString) LenThirdString;

    Reply
  • Silvio Bassi
    May 13, 2013 1:23 pm

    Quiz 1: REPLICATE function truncates the return value at 8,000 bytes.
    Quiz 2: This is the full answer.
    DECLARE @FirstString VARCHAR(MAX)
    DECLARE @SecondString VARCHAR(MAX)
    DECLARE @ThirdString VARCHAR(MAX)
    DECLARE @FourthString VARCHAR(MAX)
    DECLARE @FifthString VARCHAR(MAX)
    SET @FirstString = REPLICATE(‘A’,4000)
    SELECT LEN(@FirstString) LenFirstString;
    SET @SecondString = REPLICATE(‘B’,8000)
    SELECT LEN(@SecondString) LenSecondString;
    SET @ThirdString = REPLICATE(‘C’,8000)
    SELECT LEN(@ThirdString) LenThirdString;
    SET @FourthString = REPLICATE(‘C’,3000)
    SELECT LEN(@FourthString) LenFourthString;
    SET @FifthString = @ThirdString+@FourthString;
    SELECT LEN(@FifthString) LenFifthString;

    Reply
  • DECLARE @ThirdString VARCHAR(MAX)

    SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000);

    SELECT LEN(@ThirdString)

    varchar [ ( n | max ) ] Variable-length, non-Unicode character data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. The ISO synonyms for varchar are char varying or character varying.

    Reply
  • Sanjay Monpara
    May 13, 2013 1:44 pm

    you have to cast replicate character to varchar(max)…

    DECLARE @FirstString VARCHAR(MAX)
    DECLARE @SecondString VARCHAR(MAX)
    DECLARE @ThirdString VARCHAR(max)
    SET @FirstString = REPLICATE(‘A’,4000)
    SELECT LEN(@FirstString) LenFirstString;
    SET @SecondString = REPLICATE(‘B’,8000)
    SELECT LEN(@SecondString) LenSecondString;
    SET @ThirdString = REPLICATE(CAST(‘C’ AS varchar(MAX)),11000)
    SELECT LEN(@ThirdString) LenThirdString;

    Reply

Leave a Reply