Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
Created October 20, 2021 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmackintosh/9e4d0b63e75a6121005f8da3c4b04397 to your computer and use it in GitHub Desktop.
Save johnmackintosh/9e4d0b63e75a6121005f8da3c4b04397 to your computer and use it in GitHub Desktop.
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