Rechercher dans le manuel MySQL
B.4.4.2 Problems Using DATE Columns
The format of a DATE
value is
'YYYY-MM-DD'
. According to standard SQL, no
other format is permitted. You should use this format in
UPDATE
expressions and in the
WHERE
clause of
SELECT
statements. For example:
As a convenience, MySQL automatically converts a date to a
number if the date is used in a numeric context and vice
versa. MySQL also permits a “relaxed” string
format when updating and in a WHERE
clause
that compares a date to a DATE
,
DATETIME
, or
TIMESTAMP
column.
“Relaxed” format means that any punctuation
character may be used as the separator between parts. For
example, '2004-08-15'
and
'2004#08#15'
are equivalent. MySQL can also
convert a string containing no separators (such as
'20040815'
), provided it makes sense as a
date.
When you compare a DATE
,
TIME
,
DATETIME
, or
TIMESTAMP
to a constant string
with the <
, <=
,
=
, >=
,
>
, or BETWEEN
operators, MySQL normally converts the string to an internal
long integer for faster comparison (and also for a bit more
“relaxed” string checking). However, this
conversion is subject to the following exceptions:
For those exceptions, the comparison is done by converting the objects to strings and performing a string comparison.
To be on the safe side, assume that strings are compared as strings and use the appropriate string functions if you want to compare a temporal value to a string.
The special “zero” date
'0000-00-00'
can be stored and retrieved as
'0000-00-00'.
When a
'0000-00-00'
date is used through
Connector/ODBC, it is automatically converted to
NULL
because ODBC cannot handle that kind
of date.
Because MySQL performs the conversions just described, the
following statements work (assume that
idate
is a
DATE
column):
However, the following statement does not work:
STRCMP()
is a string function,
so it converts idate
to a string in
'YYYY-MM-DD'
format and performs a string
comparison. It does not convert '20030505'
to the date '2003-05-05'
and perform a date
comparison.
If you enable the
ALLOW_INVALID_DATES
SQL
mode, MySQL permits you to store dates that are given only
limited checking: MySQL requires only that the day is in the
range from 1 to 31 and the month is in the range from 1 to 12.
This makes MySQL very convenient for Web applications where
you obtain year, month, and day in three different fields and
you want to store exactly what the user inserted (without date
validation).
MySQL permits you to store dates where the day or month and
day are zero. This is convenient if you want to store a
birthdate in a DATE
column and
you know only part of the date. To disallow zero month or day
parts in dates, enable the
NO_ZERO_IN_DATE
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. If a date to be stored in a
DATE
column cannot be converted
to any reasonable value, MySQL stores
'0000-00-00'
. To disallow
'0000-00-00'
, enable the
NO_ZERO_DATE
mode.
To have MySQL check all dates and accept only legal dates
(unless overridden by IGNORE
), set the
sql_mode
system variable to
"NO_ZERO_IN_DATE,NO_ZERO_DATE"
.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-using-date.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.