我有鍵值對(非 Json)形式的資料,如下所示
id | Attributes
---|---------------------------------------------------
12 | Country:US, Eligibility:Yes, startDate:2022-08-04
33 | Country:CA, Eligibility:Yes, startDate:2021-12-01
11 | Country:IN, Eligibility:No, startDate:2019-11-07
我想只從 Attributes 部分中提取 startDate
預期輸出:
id | Attributes_startDate
---|----------------------
12 | 2022-08-04
33 | 2021-12-01
11 | 2019-11-07
我嘗試的一種方法是,我厭倦了通過分別在開始和結束位置附加 {、} 將輸入資料中的屬性列轉換為 JSON。還有一些如何嘗試在鍵值上添加雙引號并嘗試提取 startDate。但是,是否有任何其他有效的解決方案來提取 startDate 因為我不想依賴正則運算式。
uj5u.com熱心網友回復:
是否有任何其他有效的解決方案來提取 startDate 因為我不想依賴正則運算式。
如果您在這里對 Regex 的感覺非常強烈 - 請在下面使用
select id, split(Attribute, ':')[safe_offset(1)] Attributes_startDate
from your_table, unnest(split(Attributes)) Attribute
where trim(split(Attribute, ':')[offset(0)]) = 'startDate'
uj5u.com熱心網友回復:
在下面使用(我認為在這里使用 RegEx 是最有效的選擇)
select id, regexp_extract(Attributes, r'startDate:(\d{4}-\d{2}-\d{2})') Attributes_startDate
from your_table
如果應用于您問題中的樣本資料 - 輸出是

uj5u.com熱心網友回復:
有沒有辦法只指定鍵并提取其各自的值?(就像我們可以使用 JSON_QUERY 使用 JSON 列中的鍵提取值的方式一樣)
你可以試試下面
create temp function fakejson_extract(json string, attribute string) as ((
select split(kv, ':')[safe_offset(1)]
from unnest(split(json)) kv
where trim(split(kv, ':')[offset(0)]) = attribute
));
select id,
fakejson_extract(Attributes, 'Country') as Country,
fakejson_extract(Attributes, 'Eligibility') as Eligibility,
fakejson_extract(Attributes, 'startDate') as startDate
from your_table
如果應用于您問題中的樣本資料 - 輸出是

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/522394.html
