Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql中使用正则表达式定义新变量_Sql_Regex_Oracle_Oracle Sqldeveloper - Fatal编程技术网

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

在sql中使用正则表达式定义新变量,sql,regex,oracle,oracle-sqldeveloper,Sql,Regex,Oracle,Oracle Sqldeveloper,如果可能的话,我需要使用sql和正则表达式从“旧”列派生“新”列。我正在使用Oracle SQL Developer。 如果我在R或Python中使用regex,我将使用以下方法获得“new”列: 谢谢 使用以下命令: REGEXP_SUBSTR(old, '[1-9]{1,9}0{0,10}|[1-9]{1,5}|\b0\b') as new 这里有一个选择: SQL> with test (old) as 2 (select 'P003' from dual un

如果可能的话,我需要使用sql和正则表达式从“旧”列派生“新”列。我正在使用Oracle SQL Developer。 如果我在R或Python中使用regex,我将使用以下方法获得“new”列:

谢谢

使用以下命令:

REGEXP_SUBSTR(old, '[1-9]{1,9}0{0,10}|[1-9]{1,5}|\b0\b') as new
这里有一个选择:

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>
这里有一个选择:

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>

这可能会对“P0000010”有所帮助?你说得对,Sayan;我刚刚添加了选项并更改了顺序,以达到我上面使用的效果。这可能有助于“P0000010”如何处理?你是对的,Sayan;我只是添加了选项并更改了顺序,以达到我上面使用的效果。