SQL Server REPLICATE() Function
The REPLICATE()
function in SQL Server repeats a string a specified number of times. This is useful for creating patterns, generating test data, or for formatting output.
REPLICATE(): Definition and Usage
REPLICATE()
takes a string and an integer as input. The function returns a new string where the input string is repeated the specified number of times. This is a straightforward way to create repeated text strings. The output string will simply be a concatenation of multiple copies of the input string.
Syntax
Syntax
REPLICATE(string, integer)
Parameter Values
Parameter | Description |
---|---|
string |
The string to be repeated. This is required. |
integer |
The number of times to repeat the string. Must be a non-negative integer. This is required. |
Examples
Repeating a String Literal
This repeats the string "SQL Tutorial" five times.
Syntax
SELECT REPLICATE('SQL Tutorial', 5);
Output
SQL TutorialSQL TutorialSQL TutorialSQL TutorialSQL Tutorial
Repeating Strings from a Column
This repeats the 'CustomerName' twice for each customer in the 'Customers' table (assuming a 'Customers' table exists with a 'CustomerName' column).
Syntax
SELECT REPLICATE(CustomerName, 2)
FROM Customers;
Output
(Each CustomerName repeated twice will be displayed here.)