date_format 的语法:
date_format(date/timestamp/string ts, string fmt)
举几个例子
select date_format('2015-04-08', 'y');
2015
select date_format('2015-04-08', 'yyyy');
2015
select date_format('2015-04-08', 'yyyy-MM');
2015-04
select date_format('2015-04-08 10:10:01', 'yyyy-MM');
2015-04
select date_format('2015-04-08', 'yyyy-MM-dd');
2015-04-08
select date_format('2015/04/08', 'y');
--null,识别不了/ 这种分隔符
select date_format(regexp_replace('2015/04/08', '/', '-'), 'y');
--2015
select date_format(regexp_replace('2015/04/08', '/', '-'), 'y-MM');
--2015-04 如果有些时候需要按月聚合就需要这么搞
select month('2015/04/08');
--null 无法识别
select month(regexp_replace('2015/04/08', '/', '-'));
-- 4 可以
总结
date_format
函数如果第一个参数是字符串,连接符只能是-
,别的识别不了