MySQL uses the STR_TO_DATE() function for strings


STR_TO_DATE() function

insert image description here

In the previous ("MySQL uses the DATE_FORMAT() function for dates"), we use the date_format() function to output the date and time in the format we want. The information we receive from the user interface is transmitted in the form of strings. How to convert strings into date types for storage? The str_to_date() function can be used.

The following points need to be paid attention to when converting a string to a datetime:

  • Only numbers can appear in the string to be converted, otherwise the return result is null;
  • If the format string only contains the date, at least 8 digits are required for the string to be converted. By default, the first four digits are the year, the middle two digits are the month, and the last two digits are the date. The format string does not need to be used - to distinguish the parts of the date , the result will be automatically -spliced each part of the date;
  • The date and time after conversion must be valid, otherwise the return result is null;
  • If the string to be converted exceeds 8 digits and there is no time format in the format string, the first 8 digits will be automatically converted into a date;
  • The format string can contain a time format, and the format string does not need to use to :distinguish the various parts of the time, and the time part in the result will be automatically :connected with each part.

The usage of the str_to_date() function is basically the same as date_format(), except that the type of output data is different. The premise is that you need to be familiar with the output format, refer to the above table.

Next, give an example of the above 5 points for attention (this paragraph is for mysql, webide uses MariaDB compatible with mysql, in this environment, you may get different results if you perform the experiment described in the following 5 points):

1. If there are any characters other than numbers in the string to be converted, the characters will be automatically removed, and then 0 to 8 digits will be added to convert it into a date

MariaDB [(none)]> select str_to_date('2023050a', '%Y%m%d');
+-----------------------------------+
| str_to_date('2023050a', '%Y%m%d') |
+-----------------------------------+
| 2023-05-00                        |
+-----------------------------------+
1 row in set, 1 warning (0.000 sec)

2. If the format string only contains the date, the string to be converted needs at least 8 digits

MariaDB [(none)]> select str_to_date('202305', '%Y%m%d');
+---------------------------------+
| str_to_date('202305', '%Y%m%d') |
+---------------------------------+
| 2023-05-00                      |
+---------------------------------+
1 row in set (0.000 sec)

Result analysis: If the string 202305 is less than 8 digits, the result will be automatically filled with 0 to 8 digits.

3. The date and time after conversion must be valid, otherwise the return result is null

MariaDB [(none)]> select str_to_date('20231501', '%Y%m%d');
+-----------------------------------+
| str_to_date('20231501', '%Y%m%d') |
+-----------------------------------+
| NULL                              |
+-----------------------------------+
1 row in set, 1 warning (0.000 sec)

Result analysis: The month obtained after converting 20231501 to a date is 15, which is beyond the valid range, so the result returns null.

4. If the string to be converted exceeds 8 digits and there is no time format in the format string, the first 8 digits will be automatically converted into a date

MariaDB [(none)]> select str_to_date('2023050110', '%Y%m%d');
+-------------------------------------+
| str_to_date('2023050110', '%Y%m%d') |
+-------------------------------------+
| 2023-05-01                          |
+-------------------------------------+
1 row in set, 1 warning (0.000 sec)

5. The format string can contain a time format

MariaDB [(none)]> select str_to_date('20230501104523', '%Y%m%d%H%i%S');
+-----------------------------------------------+
| str_to_date('20230501104523', '%Y%m%d%H%i%S') |
+-----------------------------------------------+
| 2023-05-01 10:45:23                           |
+-----------------------------------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> 

おすすめ

転載: blog.csdn.net/m0_62617719/article/details/130840761