Skip to content
Advertisement

oracle: format a number with slash

I have a column like this:

Productnr
1
25
260
3700
51126

and I should write a query to display the column like this:

productnr
0/01
0/25
2/60
37/00
511/26

Advertisement

Answer

You could use:

SELECT
    Productnr,
    REGEXP_REPLACE(
        SUBSTR('000' || Productnr,
               -GREATEST(3, LENGTH(Productnr)),
               GREATEST(3, LENGTH(Productnr))),
        '(..)$',
        '/1'
    ) AS Productnr_out
FROM yourTable;

This approach first left pads the input with zeroes to a length of 3. Then, it inserts a forward slash before the final two characters using a regex replacement.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement