Open In App

REPLICATE() Function in SQL Server

Last Updated : 30 Dec, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
REPLICATE() function : This function in SQL Server is used to repeat the specified string for a given number of times. Features :
  • This function is used to return the stated string for a given number of times.
  • This function accepts only strings and integers as parameter.
  • This function returns an error if the second parameter is not provided.
  • The repeated strings returned here has no space in between the former and the latter string.
Syntax :
REPLICATE(string, integer)
Parameter : This method accepts two parameter as given below :
  • string : Specified string to repeat.
  • integer : Specified number of times for which the string is to be repeated.
Returns : It returns the stated string for a specified number of times. Example-1 : Getting the stated string for a specified number of times.
SELECT REPLICATE('Geeks for Geeks ', 2);
Output :
Geeks for Geeks Geeks for Geeks
Example-2 : Using REPLICATE() function with a variable and getting the repeated strings for the specified number of times.
DECLARE @string VARCHAR(4);  
SET @string = '123 ';  
SELECT REPLICATE(@string, 4);
Output :
123 123 123 123
Example-3 : Using REPLICATE() function with two variables and getting the repeated strings for the specified number of times.
DECLARE @string VARCHAR(6);  
DECLARE @int INT;  
SET @string = 'Geeks ';  
SET @int = 5;
SELECT REPLICATE(@string, @int);
Output :
Geeks Geeks Geeks Geeks Geeks
Example-4 : Getting the stated string for a specified number of times which is a float value.
SELECT REPLICATE('gfg ', 5.9);
Output :
gfg gfg gfg gfg gfg
Application : This function is used to repeat the specified string for a stated number of times.

Next Article
Article Tags :

Similar Reads