繁体   English   中英

如何使用 SQL 替换字符串末尾的 substring?

[英]how to replace substring at the end of string using SQL?

我在这样的表中有数据:

xxx-xxxxxxx-29
xxx-xxxxxx-87
xxx-xxxxxxxxx-2
xxx-xxx-33

我需要用 -0n 替换最后的每个 -n (在破折号和数字之间添加一个零)。 保持 -nn 不变。

所以,如果右边的第二个字符是'-',用'-0'替换它

这可以使用更新来完成吗?

我需要用-0n替换最后的每个-n (在破折号和数字之间添加一个零)。

一种选择是使用regexp_replace()

update mytable set mycol = regexp_replace(mycol, '-(\d)$', '-0\1');

'-(\d)$'匹配一个破折号,然后是字符串末尾的一个数字,并捕获该数字; 然后,您可以在前面加上前导'0'

这是一种方法:

update mytable
set data = substr(data, 1, length(data)-1) || '0' || substr(data,-1)
where data like '%-_';

暂无
暂无

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

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