2017-10-12 49 views
0

我需要從廣告系列代碼中提取日期和月份,但我的查詢只返回NULL值。日曆和月份總是遵循'mmmdd'的格式(例如,aug23,sep12,oct09),儘管廣告系列值總長度有所不同。REGEX_SUBSTR紅移

示例查詢:

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 

預先感謝您!

+0

您可以使用redshift python UDF來完全解決這個問題。如果沒有人提出正則表達式解決方案,我會幫你解決這個問題。 –

回答

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 
+1

那個伎倆......謝謝! – izzy84