--- title: "REPLICATE (Transact-SQL)" description: "REPLICATE (Transact-SQL)" author: MikeRayMSFT ms.author: mikeray ms.reviewer: "" ms.date: "03/13/2017" ms.prod: sql ms.prod_service: "database-engine, sql-database, synapse-analytics, pdw" ms.technology: t-sql ms.topic: reference ms.custom: "" f1_keywords: - "REPLICATE_TSQL" - "REPLICATE" helpviewer_keywords: - "expressions [SQL Server], repeating" - "REPLICATE function" - "repeating character expressions" dev_langs: - "TSQL" monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current" --- # REPLICATE (Transact-SQL) [!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)] Repeats a string value a specified number of times. ![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) ## Syntax ```syntaxsql REPLICATE ( string_expression , integer_expression ) ``` [!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)] ## Arguments *string_expression* Is an expression of a character string or binary data type. > [!NOTE] > If *string_expression* is of type **binary**, REPLICATE will perform an implicit conversion to **varchar**, and therefore will not preserve the binary input. > [!NOTE] > If *string_expression* input is of type **varchar(max)** or **nvarchar(max)**, REPLICATE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, *string_expression* must be explicitly cast to the appropriate large-value data type. *integer_expression* Is an expression of any integer type, including **bigint**. If *integer_expression* is negative, NULL is returned. ## Return Types Returns the same type as *string_expression*. ## Examples ### A. Using REPLICATE The following example replicates a `0` character four times in front of a production line code in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database. ```sql SELECT [Name] , REPLICATE('0', 4) + [ProductLine] AS 'Line Code' FROM [Production].[Product] WHERE [ProductLine] = 'T' ORDER BY [Name]; GO ``` [!INCLUDE[ssResult](../../includes/ssresult-md.md)] ``` Name Line Code -------------------------------------------------- --------- HL Touring Frame - Blue, 46 0000T HL Touring Frame - Blue, 50 0000T HL Touring Frame - Blue, 54 0000T HL Touring Frame - Blue, 60 0000T HL Touring Frame - Yellow, 46 0000T HL Touring Frame - Yellow, 50 0000T ... ``` ### B. Using REPLICATE and DATALENGTH The following example left pads numbers to a specified length as they are converted from a numeric data type to character or Unicode. ```sql IF EXISTS(SELECT name FROM sys.tables WHERE name = 't1') DROP TABLE t1; GO CREATE TABLE t1 ( c1 varchar(3), c2 char(3) ); GO INSERT INTO t1 VALUES ('2', '2'), ('37', '37'),('597', '597'); GO SELECT REPLICATE('0', 3 - DATALENGTH(c1)) + c1 AS 'Varchar Column', REPLICATE('0', 3 - DATALENGTH(c2)) + c2 AS 'Char Column' FROM t1; GO ``` [!INCLUDE[ssResult](../../includes/ssresult-md.md)] ``` Varchar Column Char Column -------------------- ------------ 002 2 037 37 597 597 (3 row(s) affected) ``` ## Examples: [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)] ### C: Using REPLICATE The following example replicates a `0` character four times in front of an `ItemCode` value. ```sql -- Uses AdventureWorks SELECT EnglishProductName AS Name, ProductAlternateKey AS ItemCode, REPLICATE('0', 4) + ProductAlternateKey AS FullItemCode FROM dbo.DimProduct ORDER BY Name; ``` Here are the first rows in the result set. ``` Name ItemCode FullItemCode ------------------------ -------------- --------------- Adjustable Race AR-5381 0000AR-5381 All-Purpose Bike Stand ST-1401 0000ST-1401 AWC Logo Cap CA-1098 0000CA-1098 AWC Logo Cap CA-1098 0000CA-1098 AWC Logo Cap CA-1098 0000CA-1098 BB Ball Bearing BE-2349 0000BE-2349 ``` ## See Also [SPACE (Transact-SQL)](../../t-sql/functions/space-transact-sql.md) [Data Types (Transact-SQL)](../../t-sql/data-types/data-types-transact-sql.md) [String Functions (Transact-SQL)](../../t-sql/functions/string-functions-transact-sql.md)