[MySQL] Field interception and splicing to modify data

need:

Replace the first 6 digits of a certain field in the database with a new string, and keep the other positions unchanged.

Splicing function:

CONCAT(A,B): Concatenate A and B together.

Intercept function:

LEFT(str,3): Intercept the first 3 digits of str;

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

RIGHT(str,3): Intercept the last 3 digits of str;

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

String interception: substring(str, pos); substring(str, pos, len)

  1. substring(str,4): Intercept from the 4th character position of str until the end.
select substring('sqlstudy.com', 4);
结果: | study.com |
  1. substring(str,4,2): Intercept from the 4th character position of str, only take two characters.
select substring('sqlstudy.com', 4, 2);
结果 : | st |
  1. substring(str,-4): Intercept from the 4th character from the bottom of str until the end.
select substring('sqlstudy.com', -4);
结果 : | .com |
  1. substring(str,-4,2): start intercepting from the 4th character position from the bottom of str, and only take two characters.
select substring('sqlstudy.com', -4, 2);
结果 : | .c |

PS: The string interception length cannot be a negative value.

String interception: substring_index(str,delim,count)

  1. All characters before the second '.' are intercepted.
select substring_index('www.sqlstudy.com.cn', '.', 2);
结果: | www.sqlstudy |
  1. Intercept all characters after the second '.' (reciprocal).
 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 |

application:

Replace the first 6 digits of a certain field in the database with a new string, and keep the other positions unchanged.

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

Reference link: https://blog.csdn.net/qq_41080067/article/details/127300789

Guess you like

Origin blog.csdn.net/m0_46459413/article/details/130828223