简体   繁体   English

mysql 在位置替换字符

[英]mysql replace character at position

If I have a variable in a stored procedure like this: declare str varchar(10);如果我在这样的存储过程中有一个变量:declare str varchar(10); set str = 'ABCD';设置 str = 'ABCD';

How do I change a character in the string at an arbitrary position?如何更改字符串中任意位置的字符? For example: in the procedure x is 3. I need to change str so it becomes str = 'AB*D';例如:在程序中 x 是 3。我需要改变 str 所以它变成 str = 'AB*D';

This means that, as x is equals to 3, in the string, the character at the position 3, should be changed to '*'.这意味着,由于 x 等于 3,因此在字符串中,位置 3 处的字符应更改为 '*'。

You can do this with string manipulations:你可以通过字符串操作来做到这一点:

select concat(left(str, 2), '*', substring(str, 4, 10))

Or, you can use the insert() function:或者,您可以使用insert()函数:

select insert(str, 3, 1, '*')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM