REPLICATE function of sql server with examples.

REPLICATE is used to repeat a string with specified number of times. 

Syntax of REPLICATE Function :

REPLICATE ( character_expression ,integer_expression )

character_expression is a string to repeat. 

integer_expression is number of times the character_expression to be repeated.

Return type of REPLICATE function is varchar data type.  

Examples of REPLICATE Function :

Example 1 : Use of REPLICATE function in select clause

SELECT REPLICATE('Syntax-Example', 3) 


Above example returns repeated string 3 times by specified string.

Example 2 : Use of REPLICATE function to display field value of table in a select clause

SELECT ContactName, REPLICATE(ContactName,2) 'Repeated Name' 
FROM    Customers

ContactName             Repeated Name
Clickson Andrew         Clickson Andrew Clickson Andrew
Adie Addison              Adie AddisonAdie Addison
Christopher Cole         Christopher ColeChristopher Cole

Above example repeats customers name 2 times from customers table.


No comments: