Home > Database > Mysql Tutorial > How many digits should be in a string or number to be specified as a date value by MySQL?

How many digits should be in a string or number to be specified as a date value by MySQL?

WBOY
Release: 2023-08-27 12:33:03
forward
990 people have browsed it

字符串或数字中应该有多少位数字才能被 MySQL 指定为日期值?

Although a year is treated as a 4-digit value, MySQL requires at least 8 digits in a string or number to specify it as a date value. In this case, if we also want to store microseconds, the value can be up to 20 digits.

mysql> Select TIMESTAMP('20171022040536.100000');
+-----------------------------------+
| TIMESTAMP('20171022040536100000') |
+-----------------------------------+
| 2017-10-22 04:05:36.100000        |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)
Copy after login

The above query takes a 20-digit numeric string as the TIMESTAMP value. The last 6 digits represent microseconds.

mysql> Select TIMESTAMP(20171022);
+---------------------+
| TIMESTAMP(20171022) |
+---------------------+
| 2017-10-22 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
Copy after login

The above query takes an 8-digit string as the TIMESTAMP value.

mysql> Select TIMESTAMP(201710);
+-------------------+
| TIMESTAMP(201710) |
+-------------------+
| NULL              |
+-------------------+
1 row in set, 1 warning (0.00 sec)
Copy after login

The above query returns NULL because the number has less than 8 digits.

In contrast, when treating a year as a 2-digit number, at least 6 digits are required in the string or number MySQL specifies as a date value. In this case, if we also want to store microseconds, the value can be up to 18 digits.

mysql> Select TIMESTAMP(171022);
+---------------------+
| TIMESTAMP(171022)   |
+---------------------+
| 2017-10-22 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
Copy after login

The above query takes a 6-digit string as the TIMESTAMP value.

mysql> Select TIMESTAMP('171022040536.200000');
+----------------------------------+
| TIMESTAMP('171022040536.200000') |
+----------------------------------+
| 2017-10-22 04:05:36.200000       |
+----------------------------------+
1 row in set (0.00 sec)
Copy after login

The above query takes an 18-digit numeric string as the TIMESTAMP value. The last 6 digits after the dot (.) represent microseconds.

mysql> Select TIMESTAMP(1710);
+-----------------+
| TIMESTAMP(1710) |
+-----------------+
| NULL            |
+-----------------+
1 row in set, 1 warning (0.00 sec)
Copy after login

The above query returns NULL because the number of digits is less than 6.

The above is the detailed content of How many digits should be in a string or number to be specified as a date value by MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template