TIME
This page is part of MariaDB's Documentation.
The parent of this page is: Data Types for MariaDB Xpand
Topics on this page:
Overview
Hours, minutes, seconds, and microseconds.
USAGE
TIME [(<microsecond_precision>)]
DETAILS
Data Type | Minimum Value | Maximum Value |
---|---|---|
|
|
|
Replication
When replicating from MariaDB Xpand to MariaDB Xpand, use row-based replication when using fractional seconds.
When replicating from MariaDB Xpand to MariaDB Enterprise Server, fractional seconds cannot be replicated safely.
MySQL Compatibility
As with MySQL, Xpand will interpret input values supplied to DATE
and TIME
functions and convert them to a standard format. However, unpredictable results may occur if values are provided in other formats.
Xpand matches MySQL with the exception of:
Xpand does not support casting a
TIME
to aDATETIME
and returnsNULL
.Xpand does not support casting a
TIME
to aDATE
or aDATE
to aTIME.
When converting from a
DATETIME
to aDATE
, Xpand will discard the the time portion. MySQL will take fractional seconds into account and round the time part.Casting negative value to
DATETIME
will return a zero date. MySQL returnsNULL.
When the input to
LAST_DAY
is aDATETIME
, MySQL will round to the nearestDATE
before computingLAST_DAY
where Xpand does not. For example,LAST_DAY('2013-01-31 23:59:59.999999')
returns'2017-01-31'
on Xpand and'2017-02-01'
on MySQL.If an invalid format string is supplied to
STR_TO_DATE
, Xpand will returnNULL
. MySQL ignores extra characters at the end of format string.The results when using
EXTRACT
with compound units (e.g.,DAY_SECOND
,DAY_MICROSECOND
) may exclude some of the requested units or return incorrect results.Output from datetime functions (e.g.,
NOW()
,INTERVAL() FROM_UNIXTIME()
,SUBTIME()
) display microsecond precision by default.UNIX_TIMESTAMP()
andFROM_UNIXTIME
do not support input with fractional seconds.Boolean values (
True
,False
,Null
) are accepted in aDATETIME
column. This matches the behavior for MySQL v5.5. MySQL v5.6 provides an error message.On Xpand, specifying a zero value for day, month, or year within a date will result in a zero date ('0000-00-00'). MySQL will issue an error (if the sql_
mode for NO_ZERO_IN_DATE
) is set, or allow zero value for the specific portion of the date.
Replication Compatibility
MariaDB recommends using row-based replication when utilizing fractional seconds.
Fractional seconds cannot be replicated safely from Xpand to MariaDB Enterprise Server.
EXAMPLES
Formats
Xpand supports multiple TIME
formats, including values with fractional second precision up to 6
digits (microseconds):
CREATE TABLE time_formats_example (
description VARCHAR(30),
example TIME(6)
);
INSERT INTO time_formats_example VALUES
('HH:MM:SS', '23:45:01'),
('H:M:S', '1:1:1'),
('HHMMSS', '012345'),
('HMMSS.microsec', '12345.123456'),
('SS.microsec', '21.123456');
When a TIME
value is selected, the TIME_FORMAT()
function can be used to control the output format. By default, the value is returned in the HH:MM:SS.ffffff
format, which is equivalent to TIME_FORMAT(<time>, '%H:%i:%S.%f')
:
SELECT * FROM time_formats_example;
+----------------+-----------------+
| description | example |
+----------------+-----------------+
| HH:MM:SS | 23:45:01.000000 |
| H:M:S | 01:01:01.000000 |
| HHMMSS | 01:23:45.000000 |
| HMMSS.microsec | 01:23:45.123456 |
| SS.microsec | 00:00:21.123456 |
+----------------+-----------------+
Default Microsecond Precision
The default microsecond precision when unspecified is 0
:
CREATE TABLE time_no_microseconds_example (
description VARCHAR(30),
example TIME
);
Remove Microsecond Precision
To remove microsecond precision from a TIME(6)
column, cast it to TIME
or TIME(0)
:
SELECT description,
CONVERT(example, TIME) AS example,
CONVERT(example, TIME(0)) AS example2
FROM time_formats_example;
+----------------+----------+----------+
| description | example | example2 |
+----------------+----------+----------+
| HH:MM:SS | 23:45:01 | 23:45:01 |
| H:M:S | 01:01:01 | 01:01:01 |
| HHMMSS | 01:23:45 | 01:23:45 |
| HMMSS.microsec | 01:23:45 | 01:23:45 |
| SS.microsec | 00:00:21 | 00:00:21 |
+----------------+----------+----------+
Range
Xpand does not reject out-of-range values for the TIME
data type. Out-of-range TIME
values are always accepted and replaced with valid values, even when sql_mode=STRICT_TRANS_TABLES
is set. If a value is too low, it is replaced with the minimum value. If a value is too high, it is replaced with the maximum value.
CREATE TABLE time_range_example (
description VARCHAR(30),
example TIME(6)
);
INSERT INTO time_range_example VALUES
('Minimum time', '-838:59:59.999999'),
('Maximum time', '838:59:59.999999'),
('Below minimum range', '-10000:00:00'),
('Above maximum range', '1000:00:00');
SELECT * FROM time_range_example;
+---------------------+-------------------+
| description | example |
+---------------------+-------------------+
| Minimum time | -838:59:59.000000 |
| Maximum time | 838:59:59.000000 |
| Below minimum range | -838:59:59.000000 |
| Above maximum range | 838:59:59.000000 |
+---------------------+-------------------+
Type Casting
Xpand does not support casting a DATE
to a TIME
and a TIME
to a DATE
, and returns 00:00:00
and NULL
respectively:
SELECT CAST(DATE(NOW()) AS TIME);
+---------------------------+
| CAST(DATE(NOW()) AS TIME) |
+---------------------------+
| 00:00:00 |
+---------------------------+
SELECT CONVERT(TIME(NOW()), DATE);
+----------------------------+
| CONVERT(TIME(NOW()), DATE) |
+----------------------------+
| NULL |
+----------------------------+
Xpand does not support casting a TIME
to a DATETIME
and returns NULL
:
SELECT CAST(TIME(NOW()) AS DATETIME);
+-------------------------------+
| CAST(TIME(NOW()) AS DATETIME) |
+-------------------------------+
| NULL |
+-------------------------------+