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

在我的数据库MySQL上,我有以下字符串:

19-003950

从此字符串中,我只需要提取

003950

并转换为:

103950

将字符串中的第一个字符“ 0”替换为“ 1”。

我已经成功尝试了以下SQL查询:

mysql> SELECT
    REPLACE (
        SUBSTRING_INDEX('19-003950','-',- 1),SUBSTRING(
            SUBSTRING_INDEX('19-003950',1,1
        ),'1'
    ) AS NEWSTRING;
+-----------+
| NEWSTRING |
+-----------+
| 113951    |
+-----------+
1 row in set

请你能帮我吗?

ly13644593292 回答:在MySQL中替换字符串中的第一个字符

这应该做到:

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'

Demo on DB Fiddlde

| new_string |
| :--------- |
| 103950     |
,

这是您的查询。

select concat('1',right(reverse(substring_index(reverse('19-003950'),1)),length(substring_index(reverse('19-003950'),1) - 1)))
本文链接:https://www.f2er.com/3104213.html

大家都在问