【问题标题】:how to check for invalid dates in MariaDB如何检查 MariaDB 中的无效日期
【发布时间】:2022-01-11 07:01:54
【问题描述】:

我想检查表中日期时间列中数据的有效性。我尝试使用 ISDATE() 函数,但 Maria DB 不支持它,还有哪些其他选项可以检查 Maria DB 中 datetime 列的有效性?

【问题讨论】:

  • 请注明“有效期”。你想验证什么?
  • 如果有任何值超出范围或时间戳值无效

标签: validation mariadb


【解决方案1】:

除非启用 sql 模式“ALLOW_INVALID_DATES”,否则 MariaDB 默认检查日期时间值是否有效。 (见DATETIME in MariaDB Documentation

由于 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2010-10-25
    相关资源
    最近更新 更多