T-SQL REPLICATE() Function
The T-SQL (Transact-SQL) REPLICATE() function repeats a string a specified number of times.
Syntax
REPLICATE(string, number)
Parameters
string |
Required. Specify the string to repeat. |
number |
Required. Specify the number of times to repeat the string. |
Return Value
Returns the repeated version of the specified string.
Example 1:
The example below shows the usage of REPLICATE() function.
SELECT REPLICATE('A', 3); Result: 'AAA' SELECT REPLICATE(1, 5); Result: 11111 SELECT REPLICATE('ABC', 2); Result: 'ABCABC' SELECT REPLICATE('ABC', 0); Result: '' SELECT REPLICATE(' ', 5); Result: ' '
Example 2:
Consider a database table called Employee with the following records:
EmpID | Name | City |
---|---|---|
1 | John | London |
2 | Marry | New York |
3 | Jo | Paris |
4 | Kim | Amsterdam |
5 | Ramesh | New Delhi |
6 | Huang | Beijing |
In the query below, the REPLICATE() function is used to get the repeated value of EmpID column value of Employee table.
SELECT *, REPLICATE(EmpID, 3) AS NewEmpID FROM Employee;
This will produce the result as shown below:
EmpID | Name | City | NewEmpID |
---|---|---|---|
1 | John | London | 111 |
2 | Marry | New York | 222 |
3 | Jo | Paris | 333 |
4 | Kim | Amsterdam | 444 |
5 | Ramesh | New Delhi | 555 |
6 | Huang | Beijing | 666 |
❮ T-SQL Functions