如何从 datetime
字段中获取年和月值仅作为 yyyy-mm
格式?
我试过了
Select EXTRACT(YEAR_MONTH From task_completion) from task;
但这会导致 201901
格式。
我还尝试了来自 this post 的解决方案:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, task_completion), 0)
FROM task
这会导致 Incorrect parameter count in the call to native function 'DATEDIFF'
错误,因为这是针对 sql-server
的解决方案。
有没有mysql的等效解决方案?
最佳答案
mysql年月函数
select year(task_completion),month(task_completion) from task
但是如果你需要年月使用DATE_FORMAT
select DATE_FORMAT(task_completion,'%Y-%m') from task
关于mysql - 从日期字段mysql中提取年和月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55352826/