Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

SQL Server - Beware of replicate function

May 31 2011 12:40AM by Ramireddy   

Replicate() function can be used to replicate characters. suppose replicate('a',5) will returns "aaaaa"

When you are using replicate function, if it returns more than 8000 characters, it will truncate that. To prevent that, you need to cast to nvarchar(max) or varchar(max)

Incorrect : select RIGHT(REPLICATE('a',8000) + 'b',1)
Correct : select RIGHT(CAST( REPLICATE('a',8000) as varchar(max)) + 'b',1)

Read More..   [0 clicks]

Published under: SQL Server Tips ·  ·  ·  · 


Ramireddy
2 · 41% · 12972
11
 
5
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

10  Comments  

  • This in-fact happens to almost every string function. See how the concatenation goes wrong.

    DECLARE @s1 VARCHAR(5000) = REPLICATE('a', 5000)
    DECLARE @s2 VARCHAR(5000)= REPLICATE ('a', 5000)
    
    SELECT LEN(@s1 + @s2) AS length
    /*
    length
    -----------
    8000 <== WRONG! WRONG!
    */
    
    SELECT LEN( CAST(@s1 AS VARCHAR(MAX)) + @s2) AS Length
    /*
    Length
    --------------------
    10000
    */
    

    Edit: Based on Martin's comments

    commented on May 31 2011 12:45AM
    Jacob Sebastian
    1 · 100% · 32004
  • yessss..........Its the case with other string functions also.....

    commented on May 31 2011 1:31AM
    Ramireddy
    2 · 41% · 12972
  • I had a blogpost about it here http://beyondrelational.com/blogs/madhivanan/archive/2008/07/15/varchar-max-datatype-and-replicate-function.aspx

    commented on May 31 2011 3:38AM
    Madhivanan
    3 · 39% · 12472
  • @Jacob - Your example with LEN is not a good one.

    The issue there is that the result of the concatenation (@s1 + @s2) doesn't get automatically cast to varchar(max). Nothing to do with the LEN function.

    commented on May 31 2011 6:16AM
    Martin Smith
    645 · 0% · 52
  • @Martin - You are right; it is not the best example we could show :-). However, the intention was to demonstrate the same behavior you pointed out and I thought it was apparent from what I posted.

    I edited the original post.

    commented on May 31 2011 6:32AM
    Jacob Sebastian
    1 · 100% · 32004
  • Probably to make it clear, Its not the len() function that caused the problem. Its operator precedence in "concatenation" operation that caused the problem. In Jacob's example. the datatypes are varchar(5000). So, while concatenating it estimated that result will be varchar(8000). So, it truncated remaining text.

    DECLARE @s1 VARCHAR(max) = REPLICATE('a', 8000)
    DECLARE @s2 VARCHAR(max)= REPLICATE ('a', 8000)
    DECLARE @s3 VARCHAR(max)= REPLICATE ('a', 2000)
    
    select LEN(@s1+@s2+@s3)
    

    Above example works fine. Here the datatypes are varchar(max). So, the result also it estimates that it will be varchar(max) and will correctly do the work.

    commented on May 31 2011 6:44AM
    Ramireddy
    2 · 41% · 12972
  • I saw recently a similar question asked - wondering if it's somehow related? http://tek-tips.com/viewthread.cfm?qid=1650790&page=1

    ??

    commented on Jun 7 2011 9:05AM
    Naomi
    33 · 6% · 1774
  • Never tried it (or encountered a similar situation), but now that I think about it, it is perfectly logical that every string function will demonstrate a similar behaviour.

    commented on May 20 2012 2:20AM
    Nakul Vachhrajani
    4 · 33% · 10680
  • Can any one here point out a business use-case where you would want to replicate more than 8000 instances of the same character?

    commented on Aug 12 2012 8:07PM
    Marc Jellinek
    97 · 2% · 556

Your Comment


Sign Up or Login to post a comment.

"SQL Server - Beware of replicate function" rated 5 out of 5 by 11 readers
SQL Server - Beware of replicate function , 5.0 out of 5 based on 11 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]