Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MySql中,如何将month-year转换为month-word?_Mysql_Date_Monthcalendar - Fatal编程技术网

在MySql中,如何将month-year转换为month-word?

在MySql中,如何将month-year转换为month-word?,mysql,date,monthcalendar,Mysql,Date,Monthcalendar,我使用的是MySql 5.5.37。我有一列年月号,就像表格一样 201601 我该如何将其转换为月份,然后是年份?也就是说,上述内容将转换为 January, 2016 ? 第一次转换使用str\u to\u date(),第二次转换使用date\u format()。四位数年份的格式字符为%Y,两位数月份的格式字符为%m,月份字符串的格式字符为%m(如“一月”) 输出将是 | 2016-01-00 | January, 2016 | 基本上,首先使用STR_TO_DATE()函数,在表

我使用的是MySql 5.5.37。我有一列年月号,就像表格一样

201601
我该如何将其转换为月份,然后是年份?也就是说,上述内容将转换为

January, 2016
?

第一次转换使用
str\u to\u date()
,第二次转换使用
date\u format()
。四位数年份的格式字符为
%Y
,两位数月份的格式字符为
%m
,月份字符串的格式字符为
%m
(如“一月”)

输出将是

| 2016-01-00 | January, 2016 |

基本上,首先使用STR_TO_DATE()函数,在表中提供模式并检索数据值,然后根据需要使用DATE_format()函数对其进行格式化

SELECT DATE_FORMAT(STR_TO_DATE(field_name, '%Y%m'), '%M, %Y') 
FROM table_name
字段是字符串还是数字并不重要。

使用该函数

范例

select date_format('2016-01-01', '%M, %Y');
-- returns January, 2016
select str_to_date('20160101', '%Y%m%d');
-- returns 2016-01-01
需要将
201601
转换为datetime对象,使用函数。请注意,
201601
将需要一个月日组件,我们可以通过
01
将其添加到该组件中,使其成为
20160101

范例

select date_format('2016-01-01', '%M, %Y');
-- returns January, 2016
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