问题是编写SQL脚本以删除MySQL数据库表中的前导零,该表代表一列标识(数字和点)。 我想将db中的值从示例“ 1.2.03.4”更新为“ 1.2.3.4”
答案 0 :(得分:0)
您也可以尝试replace
。似乎是IP地址,因此您可以在需要删除0
后检查所有.
,即:.0
替换为'。
select replace ( @str, '.0','.')
答案 1 :(得分:0)
MySQL 8+具有regexp_replace()
,它完全可以满足您的要求:
select regexp_replace('1.2.03.00004', '[.]0+', '.')
编辑:
如果只想替换 third 值上的前导零,则可以使用substring_index()
:
select concat_ws('.',
substring_index(col, '.', 2),
substring_index( substring_index(col, '.', 3), '.', -1 ) + 0, -- convert to number
substring_index(col, '.', -1)
)