I am trying to calculate the difference in months between two dates in R using dbplyr package, I want to send the sql query to calculate it using "timestampdiff" native function in mysql but I'm getting an error:
library(tidyverse)
library(lubridate)
library(dbplyr)
db_df <- tbl(con, "creditos")
db_df %>% mutate(diff_month = timestampdiff(month, column_date_1, column_date_2))
but the parameter month
is not been translating correctly because it looks like an object or function in R:
Error in UseMethod("escape") : no applicable method for 'escape' applied to an object of class "function"
And if written this way:
db_df %>% mutate(diff_month = timestampdiff("month", column_date_1, column_date_2))
I will also get an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
month
,column_date_1
,column_date_2
) ASdiff_month
And I believe this is because dbplyr is writing "month" with double quotes into mysql, and it should be without double quotes, something like this:
TIMESTAMPDIFF(month, column_date_1, column_date_2) AS `diff_month`
Or is there a better way to calculate month difference using dbplyr?