[英]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.