我有一个字符串,例如 X1。我需要通过在其中插入零来创建一个新字符串。更具体地说:
for X1 the new string will be X0001;
for X10 the new string will be X0010;
for X100 the new string will be X0100;
for X1000 the new string will be X1000.
The length of the new string is 5.
我试过这样做:
select substr('X1', 1, 1) || '000' || substr('X1', 2) from dual;
但这只解决了第一种情况并返回 X0001。
最佳答案
尝试将 LPAD
与 SUBSTR
一起使用:
SELECT
col,
SUBSTR(col, 1, 1) || LPAD(SUBSTR(col, 2), 4, '0') AS col_padded
FROM yourTable;
Demo
关于sql - 如何在字符串中插入零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57425556/