sql server replace 的使用方法


Sql Server REPLACE函數的使用

 

REPLACE
用第三個表達式替換第一個字符串表達式中出現的所有第二個給定字符串表達式。

語法
REPLACE ( ''string_replace1'' , ''string_replace2'' , ''string_replace3'' )

參數
''string_replace1''

待搜索的字符串表達式。string_replace1 可以是字符數據或二進制數據。

''string_replace2''

待查找的字符串表達式。string_replace2 可以是字符數據或二進制數據。

''string_replace3''

替換用的字符串表達式。string_replace3 可以是字符數據或二進制數據。

返回類型
如果 string_replace(1、2 或 3)是支持的字符數據類型之一,則返回字符數據。如果 string_replace(1、2 或 3)是支持的 binary 數據類型之一,則返回二進制數據。

示例
下例用 xxx 替換 abcdefghi 中的字符串 cde。

SELECT REPLACE(''abcdefghicde'',''cde'',''xxx'')GO
下面是結果集:

------------abxxxfghixxx(1 row(s) affected)

 

 

 

 

 

 

 

 

 

還有從某一列的值中替換的功能

demo 如下

 

 

create table #table (
id int PRIMARY  key IDENTITY(1,1),
msg VARCHAR(30)



)
INSERT into #table (msg) VALUES('a')
INSERT into #table (msg) VALUES('b')
INSERT into #table (msg) VALUES('c')
INSERT into #table (msg) VALUES('d')
INSERT into #table (msg) VALUES('e')
INSERT into #table (msg) VALUES('f')




update #table set msg =REPLACE(msg, 'c','aa')


SELECT *from #table



 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM