简体   繁体   English

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

[英]How to get a specific substring from a column in sql

I have a Orders table that has one of the columns called "details" as: 我有一个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.

There are different cell values in that column. 该列中有不同的单元格值。 Also could be like: 也可能是这样的:

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

Or anything else. 或其他任何东西。

I just want to extract the Order ID(s): 112-0654231-9637802 part from it. 我只是想从中提取Order ID(s): 112-0654231-9637802 How do I do that? 我怎么做?

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

To just get the number you can wrap the number in a capture group: 要获取数字,您可以将数字包装在捕获组中:

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

Or, if you do not have anything else with the same 3-digit, dash, 7-digit, dash, 7-digit format: 或者,如果您没有其他任何具有相同的3位数,短划线,7位数,短划线,7位数格式的内容:

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

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

select substr(detail, 160, 31)

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

REGEXP_LIKE This function searches a character column for a pattern. REGEXP_LIKE此函数在字符列中搜索模式。 Use this function in the WHERE clause of a query to return rows matching the regular expression you specify. 在查询的WHERE子句中使用此函数可返回与指定的正则表达式匹配的行。

and similar functions 和类似的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM