mysql的replace()函数介绍【mysql函数】
?
#下面就是對這個函數(shù)的簡要介紹以及范例。
?
#示例:
#比如你要將?表?tb1里面的?f1字段的abc替換為def:
| 1 | UPDATE?tb1?SET?f1=REPLACE(f1,?'abc',?'def'); |
?
?
#函數(shù)的本用法:
REPLACE(目標(biāo)字符串,搜索字符串,替換字符串);
?
#在字符串?str?中所有出現(xiàn)的字符串?from_str?均被?to_str替換,然后返回這個字符串:
| 1 2 3 4 | REPLACE(str,from_str,to_str); ???? SELECT???REPLACE('www.mysql.com',???'w',???'Ww');? ?'WwWwWw.mysql.com'//替換后的 |
?
#mysql?中的replace用法
#用法1.replace?into
| 1 | replace?into?replace?into?table?(id,name)?values('1','aa'),('2','bb'); |
?
此語句的作用是向表table中插入兩條記錄。
如果主鍵id為1或2不存在,就相當(dāng)于?
| 1 | insert?into?table?(id,name)?values('1','aa'),('2','bb') |
如果存在相同的值則不會插入數(shù)據(jù)
?
#用法2.replace(object,search,replace)
把object中出現(xiàn)search的全部替換為
| 1 | select?replace('www.163.com','w','Ww')?WwW?wWw.163.com |
?
例:把表table中的name字段中的?aa替換為bb
| 1 | update?table?set?name=replace(name,'aa','bb') |
?
Sql?Server?中?text或ntext?字段內(nèi)容替換
剛開始,Update?AA?表?Set?xx字段=Replace(xx字段,"要替換的","特定串")?,出現(xiàn)錯誤:函數(shù)?replace?的參數(shù)?1?的數(shù)據(jù)類型?ntext?無效。
| 1 2 | Update?article?set?heading=Replace(convert(nvarchar(4000),heading),'<script></script>','') update?表名?????set?text類型字段名=replace(convert(varchar(8000),text類型字段名),'要替換的字符','替換成的值') |
?
?
varchar和nvarchar類型是支持replace,所以如果你的text/ntext不超過8000/4000可以先轉(zhuǎn)換成前面兩種類型再使用replace。
?
| 1 2 | update?表名?????set?text類型字段名=replace(convert(varchar(8000),text類型字段名),'要替換的字符','替換成的值') update?表名?????set?ntext類型字段名=replace(convert(nvarchar(4000),ntext類型字段名),'要替換的字符','替換成的值') |
?
如果text/ntext超過8000/4000,看如下例子:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | declare?@pos?int ????declare?@len?int ????declare?@str?nvarchar(4000) ????declare?@des?nvarchar(4000) ????declare?@count?int? ???set?@des?='<requested_amount+1>'--要替換成的值 ?? ???set?@len=len(@des) ???set?@str=?'<requested_amount>'--要替換的字符 ?? ?? ???set?@count=0--統(tǒng)計(jì)次數(shù). ?? ?? ????WHILE?1=1 ???BEGIN ???????select?@pos=patINDEX('%'+@des+'%',propxmldata)?-?1 ???????from?表名 ???????where?條件? ?? ??????IF?@pos>=0 ??????begin ???????????DECLARE?@ptrval?binary(16) ??????????SELECT?@ptrval?=?TEXTPTR(字段名)? ??????????from?表名 ??????????where?條件 ???????????UPDATETEXT?表名.字段名?@ptrval?@pos?@len?@str ??????????set?@count=@count+1 ???????end ??????ELSE ?????????break; ???END ?? ???select?@count ? |
轉(zhuǎn)載于:https://www.cnblogs.com/heyubingfeng/archive/2013/03/13/2957852.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的mysql的replace()函数介绍【mysql函数】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新配windows服务器及上边功能的试用
- 下一篇: Java中的Annotation(2)-