Exception Handling for converting a MySQL date string to a NULL value

Source: Internet
Author: User
1. If the date format of YYYYmmdd is converted to null, the following record is displayed. One can get the value, and the other cannot get the value ** mysqlgt; SELECTDATE_FORMAT (STR_TO_DATE (lsquo; 2

1. If the date format of YYYYmmdd is converted to null, the following record is displayed. One can get the value, and the other cannot get the value ** mysqlgt; SELECT DATE_FORMAT (STR_TO_DATE (lsquo; 2

1. Convert the YYYYmmdd format to null.

As shown in the following record, one can get the value, and the other cannot get the value **
Mysql> SELECT DATE_FORMAT (STR_TO_DATE ('2014 13:01:01 ',' % Y % m % d % H: % m: % s'), '% H: % m ');
+ ------------------------- +
| DATE_FORMAT (STR_TO_DATE ('2014 13:01:01 ',' % Y % m % d % H: % m: % s'), '% H: % m') |
+ ------------------------- +
| 13:01 |
+ ------------------------- +
1 row in set (0.00 sec)

Mysql> SELECT DATE_FORMAT (STR_TO_DATE ('2014 12:55:00 ',' % Y % m % d % H: % m: % s'), '% H: % m ');
+ ------------------------- +
| DATE_FORMAT (STR_TO_DATE ('2014 12:55:00 ',' % Y % m % d % H: % m: % s'), '% H: % m') |
+ ------------------------- +
| NULL |
+ ------------------------- +
1 row in set, 1 warning (0.00 sec)

Mysql>

2. view the error message:

Mysql> show warnings;
+ --- + -- + ------------------------ +
| Level | Code | Message |
+ --- + -- + ------------------------ +
| Warning | 1411 | Incorrect datetime value: '2014 12:55:00 'for function str_to_date |
+ --- + -- + ------------------------ +
1 row in set (0.00 sec)

Mysql>
The alarm is "Incorrect datetime value: '2014 12:55:00 'for function str_to_date". The Incorrect date format is changed to a standard date format.

3. Change to % Y-% m-% d % H: % I: % s.

Mysql> select DATE_FORMAT (str_to_date ('2017-09-22 13:00:01 ',' % Y-% m-% d % H: % I: % s'), '% H: % I ');
+ -------------------------- +
| DATE_FORMAT (str_to_date ('2017-09-22 13:00:01 ',' % Y-% m-% d % H: % I: % s'), '% H: % I ') |
+ -------------------------- +
| 13:00 |
+ -------------------------- +
1 row in set (0.00 sec)

Mysql> select DATE_FORMAT (str_to_date ('2017-09-22 12:55:00 ',' % Y-% m-% d % H: % I: % s'), '% H: % I ');
+ -------------------------- +
| DATE_FORMAT (str_to_date ('2017-09-22 12:55:00 ',' % Y-% m-% d % H: % I: % s'), '% H: % I ') |
+ -------------------------- +
| 12: 55 |
+ -------------------------- +
1 row in set (0.00 sec)

Mysql>

We can see that in the standard format, the date can be converted from the string to the date format, and then the value can be obtained during the truncation.

Refer to the official website address: # function_str-to-date

4. Confirm again to narrow down the scope, which is % h. Replace the time with uppercase.

In another case, the format is changed to the preparation format of the 13:00:01 table. The score is still not obtained, as shown below:

Mysql> select DATE_FORMAT (str_to_date ('2017-09-22 13:00:01 ',' % Y-% m-% d % h: % I: % s'), '% h: % I '); + bytes + | DATE_FORMAT (str_to_date ('2017-09-22 13:00:01', '% Y-% m-% d % h: % I: % s'), '% h: % I') | + warning + | NULL | + warning + 1 row in set, 1 warning (0.00 sec) mysql> select DATE_FORMAT (str_to_date ('2017-09-22 12:55:00 ',' % Y-% m-% d % h: % I: % s'), '% h: % I '); + bytes + | DATE_FORMAT (str_to_date ('2017-09-22 12:55:00', '% Y-% m-% d % h: % I: % s'), '% h: % I') | + ------------------------------------------------------------------------------ + | + rows + 1 row in set (0.00 sec) mysql>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.