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.
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.
SELECT REPLICATE('Geeks for Geeks ', 2);
Output :
Geeks for Geeks Geeks for GeeksExample-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 123Example-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 GeeksExample-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 gfgApplication : This function is used to repeat the specified string for a stated number of times.