【问题标题】:str_to_date returning incorrect year valuestr_to_date 返回不正确的年份值
【发布时间】:2021-10-29 10:31:28
【问题描述】:

我正在尝试将 m/d/y 格式的日期转换为 yyyy-mm-dd。

select lastname, firstname, filingdate, str_to_date(filingdate, '%m/%d/%y') as reformattedDate
from Table;

对于返回的日期,月份和日期是正确的,但年份始终是 2020,即使原始日期中的年份范围是 2008 年到 2021 年。

Sample query results

我做错了什么?

【问题讨论】:

    标签: mysql date datetime str-to-date


    【解决方案1】:

    %y 只能识别年份的 2 位数字,您必须使用 %Y

    SELECT  str_to_date('01/01/2017', '%m/%d/%y'),str_to_date('01/01/2017', '%m/%d/%Y')
    
    str_to_date('01/01/2017', '%m/%d/%y') | str_to_date('01/01/2017', '%m/%d/%Y') :------------------------------------------------ | :------------------------------------------------ 2020-01-01 | 2017-01-01

    db小提琴here

    【讨论】:

    【解决方案2】:

    使用%Y 而不是%y

    select lastname, firstname, filingdate,
           str_to_date(filingdate, '%m/%d/%Y') as reformattedDate
    

    %y 是两位数的年份。 %Y 是四位数的年份。

    【讨论】:

    • 做到了。谢谢!
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2017-04-09
    • 2021-09-27
    • 2020-11-07
    相关资源
    最近更新 更多