mysql time_format 函数问题

mysql time_format function qustion

我插入和更新mysqltime_format函数来及时表达字符串值,

为什么有些数字得到

"truncated incorrect time value"

错误?

比如'55'不是查询的问题,但是当输入'188'时,出现上面的错误信息。

类型为VARCHAR(50)。

我的查询:

INSERT INTO TABLE_HOME (DURATION) 
    VALUES (TIME_FORMAT (# {DURATION, jdbcType = VARCHAR}, '% H:% i:% s'))

UPDATE TABLE_HOME SET DURATION = TIME_FORMAT (# {DURATION, jdbcType = VARCHAR}, '% H:% i:% s')

Time_Format 并非所有整数都有效..

188 不再是有效时间

即使你把它放在哪里:

"%H= Hour => 188?
 %i=Minute => 188?
 %s"=Second => 188?

你认为188Hour, Minute, Second的有效时间吗?

TIME_FORMAT() 期待时间。如果你用其他东西喂它,你首先要打石膏。在这种情况下:

mysql> SELECT CAST(55 AS TIME), CAST(188 AS TIME);
+------------------+-------------------+
| CAST(55 AS TIME) | CAST(188 AS TIME) |
+------------------+-------------------+
| 00:00:55         | NULL              |
+------------------+-------------------+
1 row in set, 1 warning (0.00 sec)

rules是:

MySQL recognizes TIME values in these formats:

  • As a string in 'D HH:MM:SS' format. You can also use one of the following “relaxed” syntaxes: 'HH:MM:SS', 'HH:MM', 'D HH:MM', 'D HH', or 'SS'. Here D represents days and can have a value from 0 to 34.

  • As a string with no delimiters in 'HHMMSS' format, provided that it makes sense as a time. For example, '101112' is understood as '10:11:12', but '109712' is illegal (it has a nonsensical minute part) and becomes '00:00:00'.

  • As a number in HHMMSS format, provided that it makes sense as a time. For example, 101112 is understood as '10:11:12'. The following alternative formats are also understood: SS, MMSS, or HHMMSS.

在这种情况下,#3 适用:

  • 55 呈现为 SS 所以它是有效的。
  • 188 不是受支持的格式,因此它生成 NULL.

日期和时间处理已经够难的了。我建议:

  1. 要明确以避免歧义(像 23:30:45 是 crystal 明确的,188 是开放的解释)。

  2. 不使用 VARCHAR 列来存储日期和时间。