我想檢查表中日期時間列中資料的有效性。我嘗試使用 ISDATE() 函式,但 Maria DB 不支持它,還有哪些其他選項可以檢查 Maria DB 中 datetime 列的有效性?
uj5u.com熱心網友回復:
除非啟用 sql 模式“ALLOW_INVALID_DATES”,否則 MariaDB 默認檢查日期時間值是否有效。(參見MariaDB 檔案中的 DATETIME
由于 MariaDB 不提供驗證函式(例如 SQL Server),因此您需要使用轉換函式,如果日期時間值不正確,則回傳 NULL:
MariaDB [test]> select id, dt from mytable;
------ ---------------------
| id | dt |
------ ---------------------
| 2 | 2001-02-31 00:00:00 |
| 3 | 2002-01-01 13:27:00 |
| 4 | 2020-12-01 00:00:00 |
| 5 | 2022-01-11 16:59:04 |
------ ---------------------
4 rows in set (0.001 sec)
MariaDB [test]> select id,dt from mytable where dayname(cast(dt as char)) is NULL;
------ ---------------------
| id | dt |
------ ---------------------
| 2 | 2001-02-31 00:00:00 |
------ ---------------------
1 row in set, 1 warning (0.001 sec)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409953.html
標籤:
