Skip to Content

SQL "Replicate" Function

Hi Experts,

Is there another function that works exactly like SQL "REPLICATE" function?

Regards

Justice

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Posted on Oct 03, 2016 at 09:01 PM

    Hi Justice,

    With this approach, I just know REPLICATE() function, but if this function is not available in your sql server version, you can build your own function to replicate data.

    CREATE FUNCTION myReplicate(@value nvarchar(max), @length int)
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
      DECLARE @count int = 0
      DECLARE @return varchar(MAX) = ''
    
      WHILE(@count < @length)
      BEGIN
      set @count = @count + 1
      set @return = @return + @value
      END
    
      RETURN @return
    END
    
    

    And after you create the function in your database, you can call like this:

    SELECT dbo.myReplicate('a', 2)
    

    Hope it helps.


    Best regards,

    Diego Lother


    View Diego Lother's profile on LinkedIn

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 03, 2016 at 10:04 PM

    Hi Diego,

    My SQL has the replicate function. What is happening is I want an alternative to this function because I used it to created a query in SAP Business UDF query and it works perfectly.

    Unfortunately SAP Business One version for HANA does not have the replicate function and became of that I can't apply the query in the UDF query .

    If there's an alternative function that also works in both SAP Business one version for SQL and HANA I will be glad if that can be shared .

    Regards

    Justice

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 03, 2016 at 10:28 PM

    Hi,

    Replicate function is working in SQL server. For example, if you run below query,

    SELECT REPLICATE( ' Nagarajan ' , 2 )

    Result is, Nagarajan Nagarajan

    Thanks

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 04, 2016 at 09:36 AM
    Hi Justice, check also Lars Breddemann in
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.