如何在多个值之间提取一个值?

问题描述

我有一列名为Concatenated Segments的列,该列具有12个细分值,并且我希望在该列上编辑公式以仅显示第5个细分。这些段由句点分隔。

Screenshot of the column value

我需要如何编辑公式才能做到这一点?

Picture of edit formula area

会使用子字符串吗?

解决方法

使用REGEXP_SUBSTR(),从输入字符串的位置1开始搜索第5个不间断的数字字符串,或第5个不间断的字符串,除了点(\d[^\.])外:

WITH
-- your input ... paste it as text next time,so I don't have to manually re-type it ....
indata(s) AS (
  SELECT '1201.0000.5611005.0099.211003.0000.2199.00099.00099.0000.0000.00000' FROM dual
)
SELECT
  REGEXP_SUBSTR(s,'\d+',1,5)    AS just_digits,REGEXP_SUBSTR(s,'[^\.]+',5) AS between_dots
FROM indata;
-- out  just_digits | between_dots 
-- out -------------+--------------
-- out  211003      | 211003
,

或者,使用旧的SUBSTR + INSTR组合

  • 可能更快处理大型数据集
  • 不关心不间断的字符串(点之间可以包含任何内容)

SQL> WITH
  2    -- thank you for typing,@marcothesane
  3  indata(s) AS (
  4    SELECT '1201.0000.5611005.0099.211003.0000.2199.00099.00099.0000.0000.00000' FROM dual
  5  )
  6  select substr(s,instr(s,'.',4) + 1,7                   instr(s,5) - instr(s,4) - 1
  8               ) result
  9  from indata;

RESULT
------
211003

SQL>