1

I have a problem to convert a script from Oracle to Postgres:

WITH q AS (
    SELECT 'ABCDE' str
    FROM dual)
SELECT 'TEST' AS WL_ID
    ,substr(str, LEVEL, 1) AS SUPPLY_MODE_ID
FROM q connect BY LEVEL < length(q.str) + 1;

I tried this in Postgres:

WITH RECURSIVE t(n) 
    AS (SELECT 'TEST', substring('ABCDE', 1, 1)
        UNION ALL
        SELECT 'TEST', substring('ABCDE', n+1,1)
        FROM t 
        where n<length('ABCDE')
)
SELECT * FROM t

I have the error :

ERROR: operator does not exist: text + integer

I don't know how to return exactly the same result as in Oracle.

MT0
  • 143,790
  • 11
  • 59
  • 117
Catalin Vladu
  • 389
  • 1
  • 6
  • 17

1 Answers1

0

This is a lot easier with Postgres:

select 'TEST' as wl_id, t.*
from unnest(string_to_array('ABCDE', null)) as t(supply_mode_id)

If you want to make sure the rows are guaranteed to be shown in the order the characters are in the string, you need an order by:

select 'TEST' as wl_id, t.supply_mode_id
from unnest(string_to_array('ABCDE', null)) with ordinality as t(supply_mode_id, idx)
order by t.idx;