Question

is it possible in PL/SQL to transform string? In my case i have VARCHARs like "COR_DIM_table1" and i qould like to get "COR_DIM_TAB_table1". Is there a away to automate such things like:

  1. Get position after "cor_dim_"
  2. Insert "tab_"
Was it helpful?

Solution

There are a number of functions you can use.

REPLACE will replace a string, INSTR (and several variants of it) can find the search string and you can do SUBSTR to break apart the string, the append the parts

select REPLACE('COR_DIM_table1','table1','tab_table1') from dual;

REPLACE('COR_DIM_TABLE1','TABL
---------------------------------

COR_DIM_tab_table1

SUBSTR( string, start_position, [ length ] )

INSTR( string, substring [, start_position [, nth_appearance ] ] )

select substr('COR_DIM_table1',1,length('COR_DIM_')) || 'tab' || substr('CO
R_DIM_table1',length('COR_DIM_')) from dual;

SUBSTR('COR_DIM_TABLE1',1,LENG
--------------------------------------------------------------------------------

COR_DIM_tab_table1

There are also corresponding REGEX_REPL, REGEX_INSTR and REGEX_SUBSTR if your naming conventions are more complex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top