Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
no nonsense interpolation of integer as part of SQL SERVER dateadd function in R
ndays <- 7
ndays_sql <- ndays * -1
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "SERVER",
Database = "TABLE",
Trusted_Connection = "True",
Port = 1433)
data_sql <- dbSendQuery(con,sqlInterpolate(con,"SELECT cols, that, I , want
FROM [server].[schema].[table1] t1
LEFT JOIN [server].[schema].[table2] t2 ON t1.common_ID = t2.common_id
WHERE Certain_Date >= DATEADD(D,?ndays2, GETDATE())
ORDER BY Certain_Date DESC",
ndays2 = ndays_sql))
data <- dbFetch(data_sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment