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:

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:
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)
154 Comments. Leave new
exec sp_configure ‘max text repl size (B)’,2147483647;
reconfigure
SET @ThirdString = REPLICATE(CONVERT(VARCHAR(MAX), ‘C’), 110000);
SELECT LEN(@ThirdString) LenThirdString;
since the input and output are of the same type, cast ‘C’ to varchar(max) before passing it into REPLICATE so the output will also be varchar(max) instead of varchar, which is limited to 8000
The solution is Casting to the type inside de Replicate Function:
SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000)
MSDN: REPLICATE ( string_expression ,integer_expression )
If string_expression is not of type varchar(max) or nvarchar(max), REPLICATE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, string_expression must be explicitly cast to the appropriate large-value data type.
So to get more than 11000 char, third expression should be like-
SET @ThirdString = REPLICATE(CAST(‘C’ AS VARCHAR(MAX)),11000)
SIr if i just change the query like this….
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(N’C’,11000)
SELECT LEN(@ThirdString) LenThirdString;
just using N while setting Third string
the result is showing 4000 for Third string..
Eagerly waiting for the answer ….
if i put value anything below 8000 in place of 11000 it gives expected result…but any thing above 8000 gives 4000 as result
I got the answer ….
Thanx :-)
REPLICATE ( string_expression ,integer_expression )
If string_expression is not of type varchar(max) or nvarchar(max), REPLICATE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, string_expression must be explicitly cast to the appropriate large-value data type.
I put the SET into two parts
SET @ThirdString = REPLICATE(‘C’ ,10000)
SET @ThirdString=@ThirdString+REPLICATE(‘C’,3000)
SELECT LEN(@ThirdString) LenThirdString;