8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

13.2.4 YEAR 类型13.2.4 The YEAR Type

YEAR 类型是一个 1 字节的类型,用于表示年份值。它可以声明为 YEAR,默认显示宽度为 4 个字符,或者等效地声明为 YEAR(4),显式指定显示宽度。The YEAR type is a 1-byte type used to represent year values. It can be declared as YEAR with an implicit display width of 4 characters, or equivalently as YEAR(4) with an explicit display width.

Note

YEAR(4) 数据类型使用显式显示宽度已被弃用,您应该预期 MySQL 的未来版本将移除对它的支持。取而代之的是使用不带显示宽度的 YEAR,其含义相同。The YEAR(4) data type using an explicit display width is deprecated and you should expect support for it to be removed in a future version of MySQL. Instead, use YEAR without a display width, which has the same meaning.

MySQL 以 YYYY 格式显示 YEAR 值,范围为 1901 到 2155,以及 0000。MySQL displays YEAR values in YYYY format, with a range of 1901 to 2155, and 0000.

YEAR 接受各种格式的输入值:YEAR accepts input values in a variety of formats:

  • 作为 4 位数字的字符串,范围为 '1901' 到 '2155'。As 4-digit strings in the range '1901' to '2155'.

  • 作为 4 位数,范围为 1901 到 2155。As 4-digit numbers in the range 1901 to 2155.

  • 作为 1 位或 2 位字符串,范围为 '0' 到 '99'。MySQL 将范围 '0' 到 '69' 和 '70' 到 '99' 的值转换为 YEAR 值,范围分别为 2000 到 2069 和 1970 到 1999。As 1- or 2-digit strings in the range '0' to '99'. MySQL converts values in the ranges '0' to '69' and '70' to '99' to YEAR values in the ranges 2000 to 2069 and 1970 to 1999.

  • 作为范围在0到99之间的一位或两位数字。MySQL 会将范围在1到69以及70到99的值分别转换为YEAR值,范围为2001到2069以及1970到1999。As 1- or 2-digit numbers in the range 0 to 99. MySQL converts values in the ranges 1 to 69 and 70 to 99 to YEAR values in the ranges 2001 to 2069 and 1970 to 1999.

    插入数字0的结果显示值为0000,内部值为0000。要插入零并将其解释为2000,请将其指定为字符串'0'或'00'。The result of inserting a numeric 0 has a display value of 0000 and an internal value of 0000. To insert zero and have it be interpreted as 2000, specify it as a string '0' or '00'.

  • 由于返回的值在 YEAR 上下文中是可接受的函数的结果,例如 NOW()。As the result of functions that return a value that is acceptable in YEAR context, such as NOW().

如果未启用严格 SQL 模式,MySQL 会将无效的 YEAR 值转换为 0000。在严格 SQL 模式下,尝试插入无效的 YEAR 值会产生错误。If strict SQL mode is not enabled, MySQL converts invalid YEAR values to 0000. In strict SQL mode, attempting to insert an invalid YEAR value produces an error.

另见 第 13.2.9 节,“日期中的两位数年份”。See also Section 13.2.9, “2-Digit Years in Dates”.