-1

I'm having this query to get how many months differ from the actual date but I receive an error. Here's my sql query: using "(month," is not working

col: id , date

SELECT DATEDIFF(month, '2022-05-15', DATE) AS test_date

error: cannot resolve 'month' given input columns:

Keyeb Yeb
  • 1
  • 1
  • 1
    Does this answer your question? [The difference in months between dates in MySQL](https://stackoverflow.com/questions/288984/the-difference-in-months-between-dates-in-mysql) – Can O' Spam May 27 '22 at 13:20
  • 1
    Use `TIMESTAMPDIFF(MONTH, ..)` – Akina May 27 '22 at 14:24

1 Answers1

0

Thank you @Can O' Spam & @Akina for your valuable input, posting it as an answer to help other community members.

To get date difference in months you can use TIMESTAMPDIFF

SELECT TIMESTAMPDIFF(UNIT,‘datetime_expr1’,‘datetime_expr2’) from table;

Returns datetime_expr2datetime_expr1

The UNIT argument is the value in which you want output of date difference in MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

Example:

SELECT TIMESTAMPDIFF(MONTH,'2022-05-15',DATE) as test_date
PratikLad-MT
  • 249
  • 5