cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SQL "Replicate" Function

millicentdark
Contributor
0 Likes
837

Hi Experts,

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

Regards

Justice

View Entire Topic
Former Member
0 Likes

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