REGEX_SUBSTR Redshift

时间:2017-10-12 03:44:00

标签: sql regex amazon-redshift

我需要从广告系列代码中提取日期和月份,但我的查询只返回NULL值。日期和月份始终遵循' mmmdd' (例如,aug23,sep12,oct09)虽然广告系列的总长度不同。

示例查询:

SELECT Campaign,
       Year,
       REGEXP_SUBSTR(campaign, '[a-z]{3}\d{2}') AS Month_Day

FROM GA_CAMPAIGN

样本表:

Campaign                                           | Year | Month_Day
TL-17-push-general-sep15septemberhighlights-0-0-0- | 17   | NULL
FA-17-stoke-aug09augusthighlights-0-0-0-0-0        | 17   | NULL

期望的输出:

Campaign                                           | Year | Month_Day
TL-17-push-general-sep15septemberhighlights-0-0-0- | 17   | sep15
FA-17-stoke-aug09augusthighlights-0-0-0-0-0        | 17   | aug09

提前谢谢!

1 个答案:

答案 0 :(得分:1)

\d无法正常工作。请尝试使用[0-9]

WITH sample AS (
          SELECT 'TL-17-push-general-sep15septemberhighlights-0-0-0' campaign
UNION ALL SELECT 'FA-17-stoke-aug09augusthighlights-0-0-0-0-0'       campaign
)
SELECT campaign,
       REGEXP_SUBSTR(campaign, '[a-z]{3}[0-9]{2}') AS Month_Day
FROM sample
;
--                     campaign                      | month_day
-----------------------------------------------------+-----------
-- TL-17-push-general-sep15septemberhighlights-0-0-0 | sep15
-- FA-17-stoke-aug09augusthighlights-0-0-0-0-0       | aug09