gpt4 book ai didi

mysql - 如何在 MySql 中将月-年转换为月份单词?

转载 作者:行者123 更新时间:2023-11-29 19:30:27 27 4
gpt4 key购买 nike

我正在使用 MySql 5.5.37。我有月年数字列,如表格

201601

如何将其转换为月份单词后跟年份?也就是说,上面的内容会被转换为

January, 2016

最佳答案

使用函数date_format .

示例

select date_format('2016-01-01', '%M, %Y');
-- returns January, 2016

并且需要将201601转换为日期时间对象,使用函数str_to_date 。请注意,201601 将需要一个月份组件,我们可以通过 concatenating 添加该组件。将 01 改为 20160101

示例

select str_to_date('20160101', '%Y%m%d');
-- returns 2016-01-01

将两者结合起来,

select date_format(str_to_date(concat('201601', '01'), '%Y%m%d'), '%M, %Y')
-- returns January, 2016

关于mysql - 如何在 MySql 中将月-年转换为月份单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41814900/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com