T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

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:

EmpIDNameCity
1JohnLondon
2MarryNew York
3JoParis
4KimAmsterdam
5RameshNew Delhi
6HuangBeijing

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:

EmpIDNameCityNewEmpID
1JohnLondon111
2MarryNew York222
3JoParis333
4KimAmsterdam444
5RameshNew Delhi555
6HuangBeijing666

❮ T-SQL Functions