In BigQuery:Bigquery:將縮短的月份字符串轉換爲數字
如何將縮短月份轉換爲月份數字?
輸入可以是:01 - 2月 - 1980年01或 - 2月 - 1980年01或-FEB 1980 輸出:02
In BigQuery:Bigquery:將縮短的月份字符串轉換爲數字
如何將縮短月份轉換爲月份數字?
輸入可以是:01 - 2月 - 1980年01或 - 2月 - 1980年01或-FEB 1980 輸出:02
這裏是純標準SQL溶液不需要UDF和依賴於PARSE_DATE功能,特別是在%b format specifier:
select extract(month from parse_date('%d-%b-%Y', d)) from
unnest(['01-FEB-1980', '01-feb-1980', '01-Feb-1980']) d
返回2, 2, 2
有時,當他們是不是在標準格式日期集成可能會非常棘手。
這是那些情況下,當日期的一個月之際,類似01 - 2月 - 1980年或01-FEB-1980年或01 - 2月 - 1980年
select
month_str as month_abbriviation,
(case
when upper(month_str) = 'JAN' then '01'
when upper(month_str) = 'FEB' then '02'
when upper(month_str) = 'MAR' then '03'
when upper(month_str) = 'APR' then '04'
when upper(month_str) = 'MAY' then '05'
when upper(month_str) = 'JUN' then '06'
when upper(month_str) = 'JUL' then '07'
when upper(month_str) = 'AUG' then '08'
when upper(month_str) = 'SEP' then '09'
when upper(month_str) = 'OCT' then '10'
when upper(month_str) = 'NOV' then '11'
when upper(month_str) = 'DEC' then '12'
end) as month_number
from (select regexp_extract('01-Feb-1980', r'..-(...)-....') as month_str);`
標準SQL的一個呼籲不錯的功能Scalar UDF
支持 兩種語言至今 - JavaScript UDF和SQL UDF
下面的例子有助於通過主查詢外提取轉換邏輯清洗裝置的代碼的感覺,所以代碼變得更好/更容易閱讀和維護(絕對物質因爲它與WHEN THEN
事情
下面這個函數的版本沉重的個人喜好)
CREATE TEMPORARY FUNCTION MONTH_STRING(x STRING)
RETURNS STRING AS (LOWER(REGEXP_EXTRACT(x, r'-(...)-')));
CREATE TEMPORARY FUNCTION MONTH_TO_NUMBER(x STRING)
RETURNS STRING AS (CASE
WHEN x='jan' THEN '01' WHEN x='feb' THEN '02' WHEN x='mar' THEN '03' WHEN x='apr' THEN '04'
WHEN x='may' THEN '05' WHEN x='jun' THEN '06' WHEN x='jul' THEN '07' WHEN x='aug' THEN '08'
WHEN x='sep' THEN '09' WHEN x='oct' THEN '10' WHEN x='nov' THEN '11' WHEN x='dec' THEN '12'
END);
WITH yourTable AS (
SELECT '1-feb-1980'AS dt UNION ALL
SELECT '01-Mar-1980' AS dt UNION ALL
SELECT '01-SEP-1980' AS dt
)
SELECT
dt,
MONTH_TO_NUMBER(MONTH_STRING(dt)) AS month_number
FROM yourTable
選項2
不知何故,我不喜歡MONTH_TO_NUMBER()函數看起來更優雅對我來說(真的不太確定 - 仍然看起來沉重 - 但我仍然喜歡試驗)
CREATE TEMPORARY FUNCTION MONTH_TO_NUMBER(x STRING)
RETURNS STRING AS ((
SELECT SUBSTR(CAST(101 + num AS STRING), 2) FROM
UNNEST(['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']) AS list
WITH OFFSET AS num WHERE list = x));
選項3
回所有內聯而不UDF
WITH yourTable AS (
SELECT '1-feb-1980'AS dt UNION ALL
SELECT '01-Mar-1980' AS dt UNION ALL
SELECT '01-SEP-1980' AS dt
)
SELECT
dt,
(SELECT SUBSTR(CAST(101 + num AS STRING), 2)
FROM UNNEST(['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']) AS list
WITH OFFSET AS num
WHERE list = LOWER(REGEXP_EXTRACT(dt, r'-(...)-'))) AS month_number
FROM yourTable
我清楚地看到現在什麼是「過度工程」的意思! :o)謝謝你莫沙!仍然喜歡試驗:o) –