欢迎光临
个人知识库,ERP、IT知识分享和应用

SQL中的replace函数

REPLACE

用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式。

语法

REPLACE ( '<em>string_expression1</em>' , '<em>string_expression2</em>' , '<em>string_expression3</em>' )

参数

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。

<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">REPLACE</span>(<span class="hljs-string">'abcdefghicde'</span>,<span class="hljs-string">'cde'</span>,<span class="hljs-string">'xxx'</span>) <span class="hljs-keyword">GO</span> 

下面是结果集:

------------ abxxxfghixxx (<span class="hljs-number">1</span> row(<span class="hljs-name">s</span>) affected) 
 
示例:

解决方法:使用replace函数

update Product set ProductName=REPLACE(ProductName,’中心回转’,’回转中心’) where ProductName like’%中心回转接头’

--批量更新 Y41 --> Y1/4
select w2.FNAME,w2.FSPECIFICATION ,* from 
T_BD_MATERIAL w1
left join T_BD_MATERIAL_L w2 on w2.FMATERIALID = w1.FMATERIALID 
left join T_BD_MATERIALSTOCK w3 on w3.FMATERIALID = w1.FMATERIALID
where w2.FNAME like '%Y41%'

UPDATE w2 SET w2.FNAME= replace (FNAME, 'Y41', 'Y1/4' ) from 
T_BD_MATERIAL w1
left join T_BD_MATERIAL_L w2 on w2.FMATERIALID = w1.FMATERIALID 
left join T_BD_MATERIALSTOCK w3 on w3.FMATERIALID = w1.FMATERIALID
where w2.FNAME like '%Y41%'
未经允许不得转载:Blog.XiaoMing.Xyz » SQL中的replace函数

登录

找回密码

注册