REPLACE
用第三個表達式替換第一個字符串表達式中出現的所有第二個給定字符串表達式。
語法
REPLACE ( 'string_expression1' , 'string_expression2' , 'string_expression3' )
參數
'string_expression1'(數據表中需中的字段名)
待搜索的字符串表達式。string_expression1 可以是字符數據或二進制數據。
'string_expression2'(數據表字段的數據中要被替換掉的內容)
待查找的字符串表達式。string_expression2 可以是字符數據或二進制數據。
'string_expression3'(用於替換數據表字段的數據中要被替換掉的內容)
替換用的字符串表達式。string_expression3 可以是字符數據或二進制數據。
返回類型
如果 string_expression(1、2 或 3)是支持的字符數據類型之一,則返回字符數據。如果 string_expression(1、2 或 3)是支持的 binary 數據類型之一,則返回二進制數據。
示例一:
下例用 xxx 替換abcdefghi 中的字符串 cde。
SELECT REPLACE('abcdefghicde','cde','xxx') GO --執行結果abxxxfghixxx
示例二:
下例是用''替換表t_actually_weather表中字段max_temperature、min_temperature中的溫度符號'℃'的
select top 5 min_temperature,max_temperature, cast(replace(max_temperature,'℃','') as DECIMAL(20,1)) as max_temperature, cast(replace(min_temperature,'℃','') as DECIMAL(20,1)) as min_temperature from t_actually_weather
執行結果如下:
6.0℃ 9.0℃ 9.0 6.0 5.3℃ 10.7℃ 10.7 5.3 5.5℃ 12.1℃ 12.1 5.5 4.4℃ 9.5℃ 9.5 4.4 4.1℃ 10.3℃ 10.3 4.1