共用方式為


複製

✅ Azure 串流分析 ✅ 網狀架構事件串流

將字串值重複指定的次數。

語法

REPLICATE ( expression , integer_expression )

論點

表示式

這是 nvarchar(max) 類型的字元表示式或數據行。

integer_expression

這是 bigint 類型的運算式。 如果integer_expression為負數,則會傳回 NULL。

傳回型別

nvarchar(max)

範例

SELECT
    ProductLine,
    CONCAT(REPLICATE('0', 4 - LEN(ProductLine), [ProductLine]) AS LineCode
FROM Input
WHERE LEN(ProductLine) = 1

CONCAT 忽略 null 值,如果 LEN(ProductLine) 超過 4,則會防止遺失值(負integer_expression會傳 REPLICATE 回 null)。

產品線 LineCode
T 0000T
1222X 1222X
99A 0099A
123456789 123456789

另請參閱