共用方式為


取代

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

將指定字串值的所有相符項目取代成另一個字串值。

語法

REPLACE ( stringExpression, stringPattern, stringReplacement )

論點

stringExpression

要搜尋的字串表達式。

stringPattern

要找到的子字串。 如果 stringPattern 是空字串,則會傳回 stringExpression

stringReplacement

取代字串。

傳回型別

如果任何自變數都是 NULL,則傳回 NULL。 否則,會傳回 nvarchar(max)。

範例

SELECT REPLACE(value, pattern, replacement) AS result FROM input
表現 結果
REPLACE('abcde', 'cd', 'xx') 'abxxe'
REPLACE('cdabcd', 'cd', 'x') 'xabx'
REPLACE('cd', 'cd', 'xxx') 'xxx'
REPLACE('abcde', 'gh', 'xx') 'abcde'
REPLACE('abcde', 'cde', '') 'ab'
REPLACE('abcde', '', 'xxx') 'abcde'
REPLACE('', 'cde', 'xxx') (空白)
REPLACE(NULL,'cde','xxx') null
REPLACE('abcde', NULL, 'xxx') null
REPLACE('abcde', 'cde', NULL) null

另請參閱