[英]Extract Substring in BigQuery
身體 |
---|
/tpt/TpScaning-ScaningRoad-PERSIVEE-202301020730327429-1567651.tp |
/tpt/TpScaning-BaliResortsInterns-PERSIVEE-202205300341154744-909890.tp |
/tpt/TpScaning-RXX-PERSIVEE-202108210412090110-000989.tp |
/tpt/TpScaning-Backnationnotecom-PERSIVEE-202112201229124214-783672.tp |
/tpt/TpScaning-DBZ-PERSIVEE-202109131129036172-908954.tp |
/tpt/TpScaning-DBZ-PERSIVEE-202109131129036172-17892578.tp |
我在 BigQuery 表 xyz 中有一列“正文”,其中包含在我們的內部應用程序上請求的掃描。 正文字段包含其中的日期部分,即(202301020730327429、202205300341154744 ...)18 位數字。
我想在名為 date 的單獨列中提取這個 18 位序列,以了解掃描請求的創建時間。
我嘗試使用 substr function 但它無法正常工作,因為 Body 字段的長度在變化。 有沒有更好的方法來實現這個?
預計 output:
日期 |
---|
202301020730327429 |
202205300341154744 |
202108210412090110 |
202112201229124214 |
202109131129036172 |
202109131129036172 |
您可以考慮以下。
WITH sample_table AS (
SELECT '/tpt/TpScaning-ScaningRoad-PERSIVEE-202301020730327429-1567651.tp' body UNION ALL
SELECT '/tpt/TpScaning-BaliResortsInterns-PERSIVEE-202205300341154744-909890.tp' body UNION ALL
SELECT '/tpt/TpScaning-RXX-PERSIVEE-202108210412090110-000989.tp' body UNION ALL
SELECT '/tpt/TpScaning-Backnationnotecom-PERSIVEE-202112201229124214-783672.tp' body UNION ALL
SELECT '/tpt/TpScaning-DBZ-PERSIVEE-202109131129036172-908954.tp' body UNION ALL
SELECT '/tpt/TpScaning-DBZ-PERSIVEE-202109131129036172-17892578.tp' body
)
SELECT *, PARSE_TIMESTAMP('%Y%m%d%H%M%E4S', LEFT(date, 14) || '.' || RIGHT(date, 4)) ts
FROM (
SELECT *, REGEXP_EXTRACT(body, r'\d{18}') AS date FROM sample_table
);
\d
匹配 1 個數字(相當於 [0-9]){18}
恰好與前一個標記匹配 18 次%E4S
格式元素用小數精度的數字解析秒,即00.0000 for %E4S
。 請注意,它在字符串數據中需要一個點 (.)。查詢結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.