【MySQL】字段截取拼接修改数据

需求:

将数据库中的某一个字段的前6位替换成一个新的字符串,其它位置不变。

拼接函数:

CONCAT(A,B):将A和B拼接起来。

截取函数:

LEFT(str,3):截取str的前3位;

select left('sqlstudy.com', 3);
结果:| sql |

RIGHT(str,3):截取str的后3位;

select right('sqlstudy.com', 3);
结果 : | com |

字符串截取:substring(str, pos); substring(str, pos, len)

  1. substring(str,4):从str的第4个字符位置开始截取,一直到结束。
select substring('sqlstudy.com', 4);
结果: | study.com |
  1. substring(str,4,2):从str的第4个字符位置开始截取,只取两个字符。
select substring('sqlstudy.com', 4, 2);
结果 : | st |
  1. substring(str,-4):从str倒数的第4个字符位置开始截取,一直到结束。
select substring('sqlstudy.com', -4);
结果 : | .com |
  1. substring(str,-4,2):从str倒数的第4个字符位置开始截取,只取两个字符。
select substring('sqlstudy.com', -4, 2);
结果 : | .c |

PS:字符串截取长度不可以为负值。

字符串截取:substring_index(str,delim,count)

  1. 截取第二个 ‘.’ 之前的所有字符。
select substring_index('www.sqlstudy.com.cn', '.', 2);
结果: | www.sqlstudy |
  1. 截取第二个 ‘.’ (倒数)之后的所有字符。
 select substring_index('www.sqlstudy.com.cn', '.', -2);
结果: | com.cn |
     3. 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串
 select substring_index('www.sqlstudy.com.cn', '.coc', 1);
结果: | www.sqlstudy.com.cn |

应用:

将数据库中的某一个字段的前6位替换成一个新的字符串,其它位置不变。

UPDATE `aa10` SET AAA102 = CONCAT("111111",substring(AAA102,7,6)) WHERE AAA102 like '111222%';

参考链接:https://blog.csdn.net/qq_41080067/article/details/127300789

猜你喜欢

转载自blog.csdn.net/m0_46459413/article/details/130828223