在我的数据库 MySQL 上我有这个字符串:
19-003950
从这个字符串我只需要提取
003950
并转换为:
103950
将字符串中的第一个字符“0”替换为“1”。
我试过这个 SQL 查询但没有成功:
mysql> SELECT
REPLACE (
SUBSTRING_INDEX('19-003950', '-' ,- 1),
SUBSTRING(
SUBSTRING_INDEX('19-003950', '-' ,- 1),
1,
1
),
'1'
) AS NEWSTRING;
+-----------+
| NEWSTRING |
+-----------+
| 113951 |
+-----------+
1 row in set
你能帮帮我吗?
最佳答案
考虑:
select
concat(
'1',
substring(str, locate('-', str) + 1)
) new_string
from (select '19-003950' str) t
locate('-', str)
为您提供破折号在字符串中的位置。您可以向其中添加 2
并获取从该位置到字符串末尾的所有内容。最后,在字符串的开头连接 '1'
。
| new_string | | :--------- | | 103950 |
关于mysql - 替换 MySQL 字符串中的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58852495/