MYSQL 迄今为止的 varchar

MYSQL varchar to date

我在将一列转换为日期时遇到了一些问题。

我希望将此 '01/02/98'(day, month, year) 转换为 '1998-02-01'(year, month, day)

以及如何将 '98' 转换为 1998

I want this '01/02/98'(day, month, year) to convert to '1998-02-01'(year, month, day). And also how you convert '98' to 1998.

STR_TO_DATE() 处理这两种情况。

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

此处解释了格式https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

%e 匹配月份中的第几天,数字 (0..31)
%m 匹配月份,数字 (00..12)
%y 匹配年份,数字(两位数)

查询

select str_to_date('01/02/98', '%e/%m/%y');

结果

| str_to_date('01/02/98', '%e/%m/%y') |
| ----------------------------------- |
| 1998-02-01                          |

demo