How to insert zeros into a string?

XLD*_*D_a 3 sql string oracle toad

I have a string, for example X1. I need to create a new string by inserting zeros into it. To be more specific:

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.
Run Code Online (Sandbox Code Playgroud)

I have tried to do this:

select substr('X1', 1, 1) || '000' || substr('X1', 2) from dual;
Run Code Online (Sandbox Code Playgroud)

But this solves only the first case and in returning X0001.

Tim*_*sen 5

Try using LPAD with SUBSTR:

SELECT
    col,
    SUBSTR(col, 1, 1) || LPAD(SUBSTR(col, 2), 4, '0') AS col_padded
FROM yourTable;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Demo