Home > Enterprise >  Using REGEXP_Substr in db for more than one condition
Using REGEXP_Substr in db for more than one condition

Time:02-23

Hi i'm using SQL Query in ORACLE db. How can i use the function REGEXP_SUBSTR (oracle SQL) to create a new column with name "VOIE" that contains just the type of "VOIE" and if there's not it said NULL. Here's an exemple of my data frame:

SP_PK  LIB                                         NAME
234    Voie orale, 2cpr, dès demain                jojo
298    2cpr, dès la semaine pro                    tata
302    Voie buccale, 1 gélule par jour             lobi
209    NULL                                        sola

What i want to have:

SP_PK  LIB                                         NAME    VOIE
234    Voie orale, 2cpr, dès demain                jojo    Voie orale
298    2cpr, dès la semaine pro                    tata    NULL
302    Voie buccale, 1 gélule par jour             lobi    Voie buccale
209    NULL                                        sola    NULL

CodePudding user response:

Here's one option:

SQL> with test (lib) as
  2    (select 'Voie orale, 2cpr, dès demain'    from dual union all
  3     select '2cpr, dès la semaine pro'        from dual union all
  4     select 'Voie buccale, 1 gélule par jour' from dual union all
  5     select null                              from dual union all
  6     select 'what is voie anyway?'            from dual
  7    )
  8  select lib,
  9         regexp_substr(lib, 'voie \w ', 1, 1, 'i') voie
 10  from test;

LIB                               VOIE
--------------------------------- ------------------------------
Voie orale, 2cpr, dcs demain      Voie orale
2cpr, dcs la semaine pro
Voie buccale, 1 gélule par jour   Voie buccale

what is voie anyway?              voie anyway

SQL>

CodePudding user response:

I should think that it is not necessary to use REGEXP... REGEX is very costly !

SELECT SUBSTR(LIB, instr(' ' || LIB, ' voie '), instr('LIB, ',') - instr(' ' || LIB, ' voie ')

Fera l'affaire.... PS j'ai pas Oracle sous la main !

  • Related