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)
- substring(str,4): Intercept from the 4th character position of str until the end.
select substring('sqlstudy.com', 4);
结果: | study.com |
- substring(str,4,2): Intercept from the 4th character position of str, only take two characters.
select substring('sqlstudy.com', 4, 2);
结果 : | st |
- substring(str,-4): Intercept from the 4th character from the bottom of str until the end.
select substring('sqlstudy.com', -4);
结果 : | .com |
- 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)
- All characters before the second '.' are intercepted.
select substring_index('www.sqlstudy.com.cn', '.', 2);
结果: | www.sqlstudy |
- 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