REPLICATE (Transact-SQL)REPLICATE (Transact-SQL)

本主題的適用對象: 是(從 2008年起) 的 SQL Server是Azure SQL Database是Azure SQL 資料倉儲yesParallel Data Warehouse THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

將字串值重複指定的次數。Repeats a string value a specified number of times.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

REPLICATE ( string_expression ,integer_expression )   

引數Arguments

string_expressionstring_expression
這是字元字串或二進位資料類型的運算式。Is an expression of a character string or binary data type. string_expression 可以是字元或二進位資料。string_expression can be either character or binary data.

注意

如果 string_expression 的類型不是 varchar(max)nvarchar(max) 則 REPLICATE 會將傳回值截斷為 8,000 位元組。If string_expression is not of type varchar(max) or nvarchar(max), REPLICATE truncates the return value at 8,000 bytes. 若要傳回大於 8,000 位元組的值,string_expression 必須明確轉換成適當的大數值資料類型。To return values greater than 8,000 bytes, string_expression must be explicitly cast to the appropriate large-value data type.

integer_expressioninteger_expression
這是任何整數類型的運算式,包括 bigintIs an expression of any integer type, including bigint. 如果 integer_expression 是負數,則會傳回 Null。If integer_expression is negative, NULL is returned.

傳回類型Return Types

傳回與 string_expression相同的類型。Returns the same type as string_expression.

範例Examples

A.A. 使用 REPLICATEUsing REPLICATE

下列範例會在 AdventureWorks2012AdventureWorks2012 資料庫中的產品線代碼前面重複 0 字元四次。The following example replicates a 0 character four times in front of a production line code in the AdventureWorks2012AdventureWorks2012 database.

SELECT [Name]  
, REPLICATE('0', 4) + [ProductLine] AS 'Line Code'  
FROM [Production].[Product]  
WHERE [ProductLine] = 'T'  
ORDER BY [Name];  
GO  

以下為結果集:Here is the result set.

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.B. 使用 REPLICATE 和 DATALENGTHUsing REPLICATE and DATALENGTH

下列範例會在數值資料類型轉換成字元或 Unicode 時,填補到指定的長度。The following example left pads numbers to a specified length as they are converted from a numeric data type to character or Unicode.

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  

以下為結果集:Here is the result set.


Varchar Column        Char Column  
--------------------  ------------  
002                   2    
037                   37   
597                   597  

(3 row(s) affected)  

範例: Azure SQL 資料倉儲Azure SQL Data Warehouse平行處理資料倉儲Parallel Data WarehouseExamples: Azure SQL 資料倉儲Azure SQL Data Warehouse and 平行處理資料倉儲Parallel Data Warehouse

C:使用 REPLICATEC: Using REPLICATE

下列範例會在 ItemCode 值前面重複 0 字元四次。The following example replicates a 0 character four times in front of an ItemCode value.

-- 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)SPACE (Transact-SQL)
資料類型 (Transact-SQL) Data Types (Transact-SQL)
字串函數 (Transact-SQL)String Functions (Transact-SQL)