str\ u to\ u date change year数据mysql

xkrw2x1b  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(335)

我正试图改变我的日期格式 mm/dd/yyyy 到mysql格式 yyyy-mm-dd .
但它改变了数据的年份 2020 .
这个 product_history 表格:

log_date    id      stock
10/30/2018  1001    59    
10/30/2018  1002    35   
10/30/2018  1003    54    
10/30/2018  1004    40   
10/30/2018  1005    5     
10/30/2018  1006    20   
10/30/2018  1007    69   
...

这是我的密码:

SELECT *, str_to_date(my_schema.product_history.log_date, '%m/%d/%y') as new_log_date FROM my_schema.product_history

输出:

log_date    id      stock new_log_date
10/30/2018  1001    59    2020-10-30
10/30/2018  1002    35    2020-10-30
10/30/2018  1003    54    2020-10-30
10/30/2018  1004    40    2020-10-30
10/30/2018  1005    5     2020-10-30
10/30/2018  1006    20    2020-10-30
10/30/2018  1007    69    2020-10-30
...
okxuctiv

okxuctiv1#

%y 用两位数表示一年。既然你有四个数字,你应该用 %Y 取而代之的是:

SELECT *, str_to_date(my_schema.product_history.log_date, '%m/%d/%Y') AS new_log_date 
-- Here ----------------------------------------------------------^
FROM   my_schema.product_history

相关问题