Home > Database > Mysql Tutorial > How does MySQL interpret numbers and strings without delimiters as dates?

How does MySQL interpret numbers and strings without delimiters as dates?

王林
Release: 2023-09-08 13:57:12
forward
1145 people have browsed it

MySQL 如何将没有分隔符的数字和字符串解释为日期?

If a string or number, even without any delimiters, in the format YYYYMMDDHHMMSS or YYMMDDHHMMSS makes sense when providing a date, then MySQL interprets this string as a valid date.

Given examples of valid and invalid dates -

mysql> Select Timestamp(20171022040536);
+---------------------------+
| Timestamp(20171022040536) |
+---------------------------+
| 2017-10-22 04:05:36       |
+---------------------------+
1 row in set (0.00 sec)

mysql> Select Timestamp('20171022040536');
+-----------------------------+
| Timestamp('20171022040536') |
+-----------------------------+
| 2017-10-22 04:05:36         |
+-----------------------------+
1 row in set (0.00 sec)

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

The above query shows examples of valid strings as well as numbers that MySQL can interpret as date values.

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

The above query is an example of an invalid string. MySQL returns NULL because the string has the wrong value (85) within one minute.

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

In the above query, MySQL returns NULL because the value of the string month (13) is wrong.

The above is the detailed content of How does MySQL interpret numbers and strings without delimiters as dates?. 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