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.
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 range1901to2155.作为 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'toYEARvalues in the ranges2000to2069and1970to1999.作为范围在
0到99之间的一位或两位数字。MySQL 会将范围在1到69以及70到99的值分别转换为YEAR值,范围为2001到2069以及1970到1999。As 1- or 2-digit numbers in the range0to99. MySQL converts values in the ranges1to69and70to99toYEARvalues in the ranges2001to2069and1970to1999.插入数字
0的结果显示值为0000,内部值为0000。要插入零并将其解释为2000,请将其指定为字符串'0'或'00'。The result of inserting a numeric0has a display value of0000and an internal value of0000. To insert zero and have it be interpreted as2000, specify it as a string'0'or'00'.由于返回的值在
YEAR上下文中是可接受的函数的结果,例如NOW()。As the result of functions that return a value that is acceptable inYEARcontext, such asNOW().
如果未启用严格 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”.