求sql的update语句将某列全部值的中间几位替换

求sql的update语句:将该表中的某一列所有中文人名(非定长)第2位替换成*;以及将某一列所有id号(定长)第4到6位替换成0000.

mysql的时候可能会报update unsafe(因为没有加where)的错,通过修改参数=0可以执行,但是不知道有没有其他可以绕过参数设置的方法。

STUFF 这个函数行吗

update TABLENAME set NAME= stuff(NAME,2,1,'*')

drop FUNCTION if exists stuff;

DELIMITER $$

CREATE FUNCTION stuff(str varchar(8000),startIndex int,length int,Newstr varchar(8000)) 

RETURNS varchar(8000) 

 comment '使用字符串替换从指定位置开始指定长度的字符'

BEGIN 

return  concat(left(str,startIndex-1 ),Newstr,right(str,length(str)-LOCATE(substring

(str,startIndex,length) , str )-length+1 )) ;

END

$$   DELIMITER ; 

先创建一个stuff函数,sql server自带

 

update TABLENAME set NAME= stuff(NAME,2,1,'*'),id=stuff(id,4,3,'0000') where 1=1;

如有问题自己调整一下,where 1=1可以替换成 id=id