sql批量修改字段前缀后缀

1.增加前缀或后缀:
可以使用concat(str1,str2,…)函数。用法如下:

1.增加前缀:
update table set name = concat('prefix_',name);
2.增加后缀:
update table set name = concat(name,'_suffix');
3.同时加前缀以及后缀:
update table set name = concat('prefix_',name,'_suffix');

2.去掉前缀或者后缀:
去掉前缀:
可以使用substr()函数(substring)或者replace()函数
substr(str,pos):
str:要截取的字符串;pos:截取开始位置,从1开始。用法如下:

update table set name = SUBSTR(name,8) ;

去掉后缀:
可以使用 substring_index(str,delim,count):delim:限定符;count:第几次;
以下为取第一次出现’_suffix’之前的所有字符,如果没找到限定符,则返回整个str。

update table set name = SUBSTRING_INDEX(name,'_suffix',1);

replace()函数可以去掉前缀或后缀:
REPLACE(str,from_str,to_str):
str:要替换的字符串;from_str:要替换的字段;to_str:替换成的字段;
用法如下:

update table set name = REPLACE(name,'prefix_','')

posted @ 2022-11-02 11:06  桂长江  阅读(1948)  评论(0)    收藏  举报