SQL 使用 regexp_subtr 提取日期子字符串

SQL extracting date substring using regexp_subtr

我有一个名为 signal_notes 的字段,其中包含如下字符串(这将是 signal_notes 的单个值):

"{ ^search_date^: ^2021-01-05^, 
^filing_date^: ^^, 
^expiry_date^: ^^, 
^other_liens^: ^^, 
^who_1st_positon^: ^^, 
^who_2nd_position^: ^^, 
^who_3rd_position^: ^^, 
^priority_from_1^: ^^, 
^priority_from_2^: ^^, 
^priority_from_3^: ^^, 
^notes^: ^^ 
^client_facing_notes^: ^^ 
 }"

有时,^expiry_date^ 行的日期格式为 'YYYY-MM-DD'.

我的新字段 expiry_date 最好采用 'YYYY-MM-DD' 格式,日期字符串来自 signal_notes 字段。

这是我目前得到的,但是returns什么都没有。

select
(regexp_substr(signal_notes, 'expiry_date [0-9-]*' )) as expiry_date
from db

我也试过了

(regexp_substr( signal_notes, '^expiry_date^: ^[0-9-]*^' )) as first_as_of_date_context

结果相同

欢迎任何建议

如果我没理解错的话,你需要一个子表达式。 ^ 是一种痛苦,一种解决方法是:

regexp_substr(signal_notes, '.expiry_date.: .([0-9-]*).', 1, 1, 'e')

这与您上次的尝试非常相似,只是它有子表达式,所以它应该只有 return 日期。

您还应该能够使用 \ 作为转义字符:

regexp_substr(signal_notes, '\^expiry_date\^: \^([0-9-]*)\^', 1, 1, 'e')