简体   繁体   English

从日期字段mysql中提取年和月

[英]extract year and month from date field mysql

How can I get year and month values only as yyyy-mm format from datetime field? 如何从datetime字段仅以yyyy-mm格式获取年和月值?

I tried 我试过了

Select EXTRACT(YEAR_MONTH From task_completion) from task;

But this results in 201901 format. 但这会产生201901格式。

Also I tried this solution from this post : 我也从这篇文章尝试了这个解决方案:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, task_completion), 0)
FROM task

Which gives Incorrect parameter count in the call to native function 'DATEDIFF' error, since this was solution for sql-server . 这会Incorrect parameter count in the call to native function 'DATEDIFF'错误时给出Incorrect parameter count in the call to native function 'DATEDIFF' ,因为这是sql-server解决方案。

Is there any equivalent solution for mysql? mysql是否有任何等效的解决方案?

year and month function in mysql mysql中的年和月功能

select year(task_completion),month(task_completion) from task

but if you need year with month use DATE_FORMAT 但如果您需要一年的月使用DATE_FORMAT

 select  DATE_FORMAT(task_completion,'%Y-%m') from task
SELECT CONCAT(year(task_completion), '-' ,month(task_completion)) FROM task

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM