在 AWS Athena 中,我嘗試使用日期連接資料庫中的兩個表,但其中一個表 (table2) 不干凈,并且包含不是日期的值,如下所示。
| table2.date |
| ---- |
|6/02/2021|
|9/02/2021|
|1431 BEL & 1628 BEL."|
|15/02/2021|
|and failed to ....|
|18/02/2021|
|19/02/2021|
我無法對清理這張桌子產生任何影響。
我目前的查詢是:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.operation_date = cast(date_parse(table2."date",'%d/%m/%Y') as date)
LIMIT 10;
我試過使用regex_like(col, '[a-z]'),但這仍然留下數字值,但不是日期。
如何讓查詢忽略不是日期的值?
uj5u.com熱心網友回復:
您可以使用函式包裝轉換運算式,try以在轉換失敗的情況下決議NULL。
select
try(date_parse(col, '%d/%m/%Y'))
from(values
('6/02/2021'),
('9/02/2021'),
('1431 BEL & 1628 BEL.'),
('15/02/2021'),
('and failed to ....'),
('18/02/2021'),
('19/02/2021')
) as t(col)
| # | _col0 |
|---|---|
| 1 | 2021-02-06 00:00:00.000 |
| 2 | 2021-02-09 00:00:00.000 |
| 3 | |
| 4 | 2021-02-15 00:00:00.000 |
| 5 | |
| 6 | 2021-02-18 00:00:00.000 |
| 7 | 2021-02-19 00:00:00.000 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/474661.html
