在SQL中使用正则表达式定义新变量

问题描述

如果可能,我需要使用sql和regex从“旧”列派生“新”列。我正在使用Oracle sql Developer。 如果我在R或Python中使用正则表达式,则可以使用此配方获取“新”列:

<home-loan-submenu [loanId]="loan.loanHeaderId" [source] = source></home-loan-submenu>

谢谢。

使用此:

[1,2,3,4,5,6,7,8,9]{1,5}|\b0\b

old              new
P003             3 
4                4 
P00005           5
P0005            5
12               12
P00000016        16
0                0

解决方法

这里是一个选择:

SQL> with test (old) as
  2    (select 'P003'      from dual union all
  3     select '4'         from dual union all
  4     select 'P00005'    from dual union all
  5     select 'P0005'     from dual union all
  6     select '12'        from dual union all
  7     select 'P00000016' from dual union all
  8     select '0'         from dual
  9    )
 10  select old,to_number(regexp_substr(old,'\d+')) new
 11  from test;

OLD              NEW
--------- ----------
P003               3
4                  4
P00005             5
P0005              5
12                12
P00000016         16
0                  0

7 rows selected.

SQL>