Oracle
 sql >> Database >  >> RDS >> Oracle

Recupero di determinate stringhe all'interno di una stringa utilizzando Oracle SQL

Puoi usare regexp_substr per suddividere i dati di input in righe e quindi cercare le stringhe pertinenti, ad esempio:

SQL> SELECT regexp_substr(line, 'aXYZApple[^,]*') subtxt
  2    FROM (SELECT regexp_substr(:x, '[^|]*\|', 1, rownum + 1) line
  3             FROM dual
  4           CONNECT BY LEVEL <= length(:x) - length(REPLACE(:x, '|', '')))
  5   WHERE regexp_substr(line || ',', '[^,]*,', 1, 2) = 'OU=Managed,'
  6     AND line LIKE '%aXYZApple%';

SUBTXT
--------------------------------------------------------------------------------
aXYZApple-Au
aXYZApple-Readonly
aXYZApple-Write

Ecco una piccola spiegazione. Devi eseguire la query passo dopo passo.

La parte interna della query scorrerà i tuoi dati (per ogni | ):

SQL> SELECT regexp_substr(:x, '[^|]*\|', 1, rownum + 1) line
  2    FROM dual
  3  CONNECT BY LEVEL <= length(:x) - length(REPLACE(:x, '|', ''));

LINE
--------------------------------------------------------------------------------
 CN=aXYZApple-Au,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=31107427,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=
 CN=ea90045052,OU=Groups,OU=eProfile,DC=core,DC=dir,DC=abc,DC=com |
 CN=S0901448,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=
 CN=00900887,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=
 CN=NSMMMM,OU=LRP,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=aXYZApple-Readonly,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=WWSWW-Au,OU=LRP,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=aLogical_RW,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=aXYZApple-Write,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |

Dovresti quindi eseguire il ciclo per OU=Managed stringa nella seconda posizione:

SQL> SELECT regexp_substr(line || ',', '[^,]*,', 1, 2) second_part
  2    FROM (SELECT regexp_substr(:x, '[^|]*\|', 1, rownum + 1) line
  3             FROM dual
  4           CONNECT BY LEVEL <= length(:x) - length(REPLACE(:x, '|', '')));

SECOND_PART
--------------------------------------------------------------------------------
OU=Managed,
OU=Distribution Lists,
OU=Groups,
OU=Distribution Lists,
OU=Distribution Lists,
OU=LRP,
OU=Managed,
OU=LRP,
OU=Managed,
OU=Managed,

Infine, seleziona la parte rilevante con un ultimo regexp_substr .