如何从sql中的列获取特定的子字符串

时间:2017-04-05 22:44:27

标签: sql oracle

我有一个Orders表,其中一列名为" details"为:

Contact ID: A18YTX7GWEJRU8    City/Site and Site Name: Orlando - Orlando (UFL4)    Date of Call (MM/DD/YYYY): 01/23/2017    Time of Call (Local Time): 16:44    Order ID(s): 112-0654231-9637802    Call Summary: Cx did not receive. Order marked as delivered to doorstep at 16:27      created by flx-cstech on behalf of sssmiley.

该列中有不同的单元格值。也可以像:

Short Description: Dry Ice Retrieval    Please enter the following information for the site ops to pick up the dry ice from the customer:    Contact ID:AD3R60PA1QCCF    Order ID:112-6254812-3186644

或其他任何事情。

我只想从中提取Order ID(s): 112-0654231-9637802部分。我该怎么做?

3 个答案:

答案 0 :(得分:1)

SELECT REGEXP_SUBSTR(
         your_column,
         'Order\s+ID(\s*\(s\))?:\s*\d{3}-\d{7}-\d{7}'
       )
FROM   your_table

要获取数字,您可以将数字包装在捕获组中:

SELECT REGEXP_SUBSTR(
         your_column,
         'Order\s+ID(\s*\(s\))?:\s*(\d{3}-\d{7}-\d{7})',
         1,                                              -- Start from the 1st character
         1,                                              -- Get the 1st match
         NULL,                                           -- Apply default flags
         2                                               -- Get the 2nd capture group
       )
FROM   your_table

或者,如果您没有其他任何相同的3位数,短划线,7位数,短划线,7位数格式:

SELECT REGEXP_SUBSTR(
         your_column,
         '\d{3}-\d{7}-\d{7}',
       )
FROM   your_table

答案 1 :(得分:0)

你的字符串看起来像一个固定的格式字符串,所以最简单的方法是:

select substr(detail, 160, 31)

答案 2 :(得分:-1)

https://docs.oracle.com/cd/B12037_01/appdev.101/b10795/adfns_re.htm

  

REGEXP_LIKE此函数在字符列中搜索模式。用这个   函数在查询的WHERE子句中返回与之匹配的行   你指定的正则表达式。

和类似的功能