Date and Time Types
839
• Conversion of values from one temporal type to another occurs according to the rules in
Section 11.1.5.7, “Conversion Between Date and Time Types”
.
• MySQL automatically converts a date or time value to a number if the value is used in a numeric
context and vice versa.
• By default, when MySQL encounters a value for a date or time type that is out of range or otherwise
illegal for the type, it converts the value to the “zero” value for that type. The exception is that out-of-
range
TIME
values are clipped to the appropriate endpoint of the
TIME
range.
• Starting from MySQL 5.0.2, by setting the SQL mode to the appropriate value, you can specify
more exactly what kind of dates you want MySQL to support. (See
Section 5.1.7, “Server SQL
Modes”
.) You can get MySQL to accept certain dates, such as
'2009-11-31'
, by enabling the
ALLOW_INVALID_DATES
[535]
SQL mode. (Before 5.0.2, this mode was the default behavior for
MySQL.) This is useful when you want to store a “possibly wrong” value which the user has specified
(for example, in a web form) in the database for future processing. Under this mode, MySQL verifies
only that the month is in the range from 1 to 12 and that the day is in the range from 1 to 31.
• MySQL permits you to store dates where the day or month and day are zero in a
DATE
or
DATETIME
column. This is extremely useful for applications that need to store birthdates for which you may not
know the exact date. In this case, you simply store the date as
'2009-00-00'
or
'2009-01-00'
.
If you store dates such as these, you should not expect to get correct results for functions such as
DATE_SUB()
[922]
or
DATE_ADD()
[919]
that require complete dates. To disallow zero month or
day parts in dates, enable the
NO_ZERO_IN_DATE
[538]
SQL mode.
• MySQL permits you to store a “zero” value of
'0000-00-00'
as a “dummy date.” This is in some
cases more convenient than using
NULL
values, and uses less data and index space. To disallow
'0000-00-00'
, enable the
NO_ZERO_DATE
[538]
SQL mode.
• “Zero” date or time values used through Connector/ODBC are converted automatically to
NULL
because ODBC cannot handle such values.
The following table shows the format of the “zero” value for each type. The “zero” values are special,
but you can store or refer to them explicitly using the values shown in the table. You can also do this
using the values
'0'
or
0
, which are easier to write. For temporal types that include a date part (
DATE
,
DATETIME
, and
TIMESTAMP
), use of these values produces warnings if the
NO_ZERO_DATE
[538]
SQL
mode is enabled.
Data Type
“Zero” Value
DATE
'0000-00-00'
TIME
'00:00:00'
DATETIME
'0000-00-00 00:00:00'
TIMESTAMP
'0000-00-00 00:00:00'
YEAR
0000
11.1.5.1. The
DATE
,
DATETIME
, and
TIMESTAMP
Types
The
DATE
,
DATETIME
, and
TIMESTAMP
types are related. This section describes their characteristics,
how they are similar, and how they differ. MySQL recognizes
DATE
,
DATETIME
, and
TIMESTAMP
values in several formats, described in
Section 9.1.3, “Date and Time Literals”
. For the
DATE
and
DATETIME
range descriptions, “supported” means that although earlier values might work, there is no
guarantee.
The
DATE
type is used for values with a date part but no time part. MySQL retrieves and displays
DATE
values in
'YYYY-MM-DD'
format. The supported range is
'1000-01-01'
to
'9999-12-31'
.
The
DATETIME
type is used for values that contain both date and time parts. MySQL retrieves
and displays
DATETIME
values in
'YYYY-MM-DD HH:MM:SS'
format. The supported range is
'1000-01-01 00:00:00'
to
'9999-12-31 23:59:59'
.
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...