我有列column_name VARCHAR2
與各種數據。將其轉換爲DATE
數據類型我寫了下面的表達式:將Oracle VARCHAR2轉換爲DATE並排除無效數據
SELECT TO_DATE(column_name, 'YYYY/MM/DD') FROM schema.table;
但它給我的錯誤:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0 01841. 00000 - "(full) year must be between -4713 and +9999, and not be 0"
*Cause: Illegal year entered
*Action: Input year in the specified range
柱包括NULL
,space
,00000000
和字符串日期,如 「20161111
」。
要排除無效數據,我決定使用DECODE
:
SELECT DECODE(column_name,
'', NULL,
'00000000', NULL,
TO_DATE(column_name, 'YYYY/MM/DD'))
FROM schema.table;
但在這種情況下,我得到了以下錯誤:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0 01841. 00000 - "(full) year must be between -4713 and +9999, and not be 0"
*Cause: Illegal year entered
*Action: Input year in the specified range
數據例如:
| # | column_name |
|---|-------------|
| 1 | 00000000 |
| 2 | |
| 3 | (null) |
| 4 | 20161111 |
什麼時我做錯了?
是否有解決方案排除所有無效的數據,而不包括在DECODE的情況下?
你能顯示一些來自列的示例數據嗎? –
請勿粘貼圖片,請使用文字。 –
你認爲'20161111'有效嗎? (即2016-11-11) –