【问题标题】:Need to write oracle sql query to extract 2 dates parsing string需要编写oracle sql查询来提取2个日期解析字符串
【发布时间】:2021-04-12 17:43:23
【问题描述】:

我需要解析一个字符串来提取示例文本的日期(有时日期格式也会改变),如下所示:

需要 2 列中 POP 或Period of Performance 词之后的日期

结果:

start date:10/1/2018
end date:9/30/2019

示例文本:

Facility:XYZ Location: NYC           
Lease date: 10/1/2020- 9/30/2020
Lessor: Edgewater Industrial Park LLC
Period of Performance: 10/1/2018- 9/30/2019
Schedule: region: xyz

或

Location: NY 
LeasedateL 10/1/2020- 9/30/2020
Lessor: Edgewater Industrial Park LLC
PoP 10/1/2018- 9/30/2019
Schedule: region: xyz

【问题讨论】:

  • 你同时标记oracle11g和oracle10g,你实际使用的是哪一个?
  • 我刚刚找到了一条记录,其中包含下面一行的文本,只有一个日期,POP through 是从 2019 年 7 月 31 日开始的 POP

标签: sql oracle oracle11g oracle10g oracle-sqldeveloper


【解决方案1】:

正则表达式可以帮助提取日期部分

SQL> with t as
  2  ( select '
  3  Facility:ASAHSN Location: LaGuardia NY
  4  Lease Number:26DDDKKD-55
  5  Lessor: Edgewater Industrial Park LLC
  6  Period of Performance: 10/1/2018- 9/30/2019
  7  Schedule: region: xyz' txt
  8  from dual
  9  )
 10  select
 11    regexp_substr(txt,'[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}.*[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}')
 12  from t;

REGEXP_SUBSTR(TXT,'[
--------------------
10/1/2018- 9/30/2019

然后您可以从那里继续获取每个日期,例如

SQL> with t as
  2  ( select '
  3  Facility:ASAHSN Location: LaGuardia NY
  4  Lease Number:26DDDKKD-55
  5  Lessor: Edgewater Industrial Park LLC
  6  Period of Performance: 10/1/2018- 9/30/2019
  7  Schedule: region: xyz' txt
  8  from dual
  9  ),
 10  t1 as
 11  (
 12  select
 13    regexp_substr(txt,'[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}.*[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}') dates
 14  from t
 15  )
 16  select
 17    substr(dates,1,instr(dates,'-')-1),
 18    substr(dates,instr(dates,'-')+1)
 19  from t1;

SUBSTR(DA SUBSTR(DAT
--------- ----------
10/1/2018  9/30/2019

然后根据需要应用 TO_DATE

【讨论】:

  • 非常感谢您的回复,实际上文本可以有多种类型的日期,所以我必须选择以 POP 或 Period of Performace 开头的日期,我该如何实现它
  • 它总是归结为只是改变正则表达式来识别你需要的适当元素。完整文档在这里docs.oracle.com/cd/B19306_01/B14251_01/adfns_regexp.htm
猜你喜欢
  • 2019-10-11
  • 2016-10-02
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多