Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active March 3, 2022 14:13
Show Gist options
  • Save rponte/d7cd8481162692694291ad5331e59031 to your computer and use it in GitHub Desktop.
Save rponte/d7cd8481162692694291ad5331e59031 to your computer and use it in GitHub Desktop.
Oracle PLSQL - splitting string by token using regexp_substr() function
--
-- splitting as columns
--
select regexp_substr('a-b-c', '[^-]+', 1, 1) as grupo_1
,regexp_substr('a-b-c', '[^-]+', 1, 2) as grupo_2
,regexp_substr('a-b-c', '[^-]+', 1, 3) as grupo_3
from dual;
-- result:
-- a b c
--
-- splitting as rows
--
select regexp_substr('a-b-c', '[^-]+', 1, level) as one_element
from dual
connect by regexp_substr('a-b-c', '[^-]+', 1, level) is not null;
-- result:
-- a
-- b
-- c
--
-- splitting rows by new line
--
with all_text as (
select 'ORA-20690: Erro ao gerar combinacao contabil para lancamento: Lancamento: { tipo: VERBA_CONTRATUAL, id: 123 }
ORA-06512: em "MDB_SGC.MDB_SGC_UTILS_PKG", line 59
ORA-06512: em "MDB_SGC.MDB_SGC_INTEGRADOR_PKG", line 22
ORA-06512: em "MDB_SGC.MDB_SGC_INTEGRADOR_PKG", line 1058
ORA-20905: Dados nao encontrados para Centro de Custo para VERBA_CONTRATUAL (DEBITO) com ID 123
ORA-06512: em "MDB_SGC.MDB_SGC_UTILS_PKG", line 59
ORA-06512: em "MDB_SGC.MDB_SGC_GERA_COMB_CONTAB_PKG", line 392
ORA-01403: dados não enco' as text
,'ORA-20690: Erro ao gerar combinacao contabil para lancamento' as text2
from dual
)
select regexp_substr(t.text, '^.*$', 1, level, 'm') as each_line
from all_text t
connect by regexp_substr(t.text, '^.*$', 1, level, 'm') is not null
;
-- result:
-- ORA-20690: Erro ao gerar combinacao contabil para lancamento: Lancamento: { tipo: VERBA_CONTRATUAL, id: 123 }
-- ORA-06512: em "MDB_SGC.MDB_SGC_UTILS_PKG", line 59
-- ORA-06512: em "MDB_SGC.MDB_SGC_INTEGRADOR_PKG", line 22
-- ORA-06512: em "MDB_SGC.MDB_SGC_INTEGRADOR_PKG", line 1058
-- ORA-20905: Dados nao encontrados para Centro de Custo para VERBA_CONTRATUAL (DEBITO) com ID 123
-- ORA-06512: em "MDB_SGC.MDB_SGC_UTILS_PKG", line 59
-- ORA-06512: em "MDB_SGC.MDB_SGC_GERA_COMB_CONTAB_PKG", line 392
--
-- splitting rows by new line and getting only the first line
--
with all_text as (
select 'ORA-20690: Erro ao gerar combinacao contabil para lancamento: Lancamento: { tipo: VERBA_CONTRATUAL, id: 123 }
ORA-06512: em "MDB_SGC.MDB_SGC_UTILS_PKG", line 59
ORA-06512: em "MDB_SGC.MDB_SGC_INTEGRADOR_PKG", line 22
ORA-06512: em "MDB_SGC.MDB_SGC_INTEGRADOR_PKG", line 1058
ORA-20905: Dados nao encontrados para Centro de Custo para VERBA_CONTRATUAL (DEBITO) com ID 123
ORA-06512: em "MDB_SGC.MDB_SGC_UTILS_PKG", line 59
ORA-06512: em "MDB_SGC.MDB_SGC_GERA_COMB_CONTAB_PKG", line 392
ORA-01403: dados não enco' as text
,'ORA-20690: Erro ao gerar combinacao contabil para lancamento' as text2
from dual
)
select regexp_substr(t.text, '^.*$', 1, 1, 'm') as first_line_only
from all_text t
;
-- result:
-- ORA-20690: Erro ao gerar combinacao contabil para lancamento: Lancamento: { tipo: VERBA_CONTRATUAL, id: 123 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment