Amazon redshift 从红移的字符串中提取日期和时间
我有一个类似下面的专栏。最后两组数字是日期和时间。我想通过从列中提取值来创建日期时间列 1002206391240385-赞助产品-SameDayPull-20190627-012313.json 开始时提取日期,但它没有提供我需要的内容Amazon redshift 从红移的字符串中提取日期和时间,amazon-redshift,Amazon Redshift,我有一个类似下面的专栏。最后两组数字是日期和时间。我想通过从列中提取值来创建日期时间列 1002206391240385-赞助产品-SameDayPull-20190627-012313.json 开始时提取日期,但它没有提供我需要的内容 Select regexp_substr('1002206391240385-sponsoredProducts-SameDayPull-20190627-012313.json','-[\\d{8}]-') 此子字符串从字符串中提取日期时间部分 SELEC
Select regexp_substr('1002206391240385-sponsoredProducts-SameDayPull-20190627-012313.json','-[\\d{8}]-')
此子字符串从字符串中提取日期时间部分
SELECT substring(col_name,regexp_instr(col_name,'-',1,regexp_count(col_name,'-')-1)+1,
regexp_instr(col_name,'.json',1)-regexp_instr(col_name,'-',1,regexp_count(col_name,'-')-1)-1)
regexp\u count
counts在字符串中有许多连字符
regexp\u instr
给出连字符的位置
substring
返回字符串中从第二个连字符到最后一个连字符的.json
为了测试我使用的
WITH test(col_name) AS (
SELECT '1002206391240385-sponsoredProducts-SameDayPull-20190627-012313.json'::TEXT
)
SELECT col_name,
substring(col_name,regexp_instr(col_name,'-',1,regexp_count(col_name,'-')-1)+1,
regexp_instr(col_name,'.json',1)-regexp_instr(col_name,'-',1,regexp_count(col_name,'-')-1)-1) datetime
FROM test
输出为
col_name datetime
1002206391240385-sponsoredProducts-SameDayPull-20190627-012313.json 20190627-012313
或者,如果文件名格式一致,则可以使用非正则表达式解决方案,例如,提取文件名字符串中包含日期的部分,然后与一起使用以提取日期和时间:
SELECT TO_TIMESTAMP(RIGHT('1002206391240385-sponsoredProducts-SameDayPull-20190627-012313.json', 20), 'YYYYMMDD-HH24MISS.json') AS extracted_datetime
返回
extracted_datetime |
----------------------|
2019-06-27 01:23:13+00|