Search code examples
databaseoracle-databaserandomreplaceoracle19c

Replace original data with random values (Oracle)


Into tableA.Names I got data:

83601 Thomas Levy
Adam Jonson

I need to replace original data with some random. I can use:

dbms_random.string('a',10) string.

and the result is:

OflRDIBSoN

How to do that the result will be like:

45618 ERGHNYU IULB.

First it should count values:

83601 - 5 letter
Thomas - 6 letter
Levy - 4 letter

and the do like dbms_random.string('a',5) + dbms_random.string('a',6) + dbms_random.string('a',4)?


Solution

  • You can break down the string into the part, Replace the words with random values and combine them again as follows:

    SELECT LISTAGG(dbms_random.string('a',LENGTH(REGEXP_SUBSTR(T.STR,'[^ ]+',1, L.LVL))), ' ') 
            WITHIN GROUP (ORDER BY L.LVL)
    FROM YOUR_DATA T 
    CROSS APPLY 
        (SELECT LEVEL AS LVL FROM DUAL CONNECT BY LEVEL <= REGEXP_COUNT(T.STR,' ') + 1) L
    GROUP BY T.STR;
    

    DB<>Fiddle