替换MySQL中字符串中的第一个字符

替换MySQL中字符串中的第一个字符,mysql,string,select,replace,substring,Mysql,String,Select,Replace,Substring,在我的数据库MySQL上,我有以下字符串: 19-003950 我只需要从这个字符串中提取 003950 并转化为: 103950 正在将字符串中的第一个字符“0”替换为“1” 我已成功尝试了此SQL查询: mysql> SELECT REPLACE ( SUBSTRING_INDEX('19-003950', '-' ,- 1), SUBSTRING( SUBSTRING_INDEX('19-003950', '-'

在我的数据库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 str, CONCAT('1', SUBSTRING(SUBSTRING_INDEX(str, '-', -1), 2))
FROM (
    SELECT '19-003950' AS str
) AS x
考虑:

select 
    concat(
        '1',
        substring(str, locate('-', str) + 1)
    ) new_string
from (select '19-003950' str) t
locate('-',str)
提供破折号在字符串中的位置。您可以将
2
添加到该位置,并从该位置获取所有内容,直到字符串结束。最后,在字符串的开头连接
'1'

| new_string | | :--------- | | 103950 | |新绳| | :--------- | | 103950 | 这是你的问题

select concat('1', right(reverse(substring_index(reverse('19-003950'),'-', 1)), 
             length(substring_index(reverse('19-003950'),'-', 1) - 1)))