You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用str_to_date转换m/d/y格式日期时返回年份值错误是什么原因

你的SQL语句错误使用了小写%y作为年份匹配符,这是导致年份异常的核心原因:

  • MySQL中%y仅适配两位数字的年份(如08代表2008、99代表1999),当你的原始filingdate字段存储的是4位年份时,%y只会读取年份的前两位数字。由于你提到的原始数据年份范围是2008~2021,所有年份的前两位都是20,%y会将其识别为2020年,就出现了月日正常、年份固定为2020的问题。
  • 4位年份需要使用大写的%Y作为匹配符。

修正后的SQL语句如下:

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

内容的提问来源于stack exchange,提问作者willwonkforfood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:06:04