Replicate Function In Sql in SQL
? REPLICATE Function in SQL
The REPLICATE() function repeats a string a specified number of times — similar to REPEAT() in MySQL, but used in SQL Server.
? Syntax (SQL Server):
REPLICATE(string_expression, integer_expression)string_expression: The string you want to repeat.integer_expression: How many times to repeat it.
? Example 1: Basic Usage
SELECT REPLICATE('SQL ', 3) AS repeated_text;Output:
| repeated_text |
|---|
| SQL SQL SQL |
? Example 2: Repeat a character
SELECT REPLICATE('*', 10) AS stars;Output:
| stars |
|---|
| ********** |
? Difference from REPEAT():
| Function | Used In |
|---|---|
REPEAT() | MySQL, PostgreSQL |
REPLICATE() | SQL Server |
? Great for:
Padding
Generating fixed-width output
Creating visual separators in console results
Would you like to see how to combine REPLICATE() with LEN() for formatting strings?