-
Notifications
You must be signed in to change notification settings - Fork 64
Description
Hi there! I love glue_sql() for safe interpolation, but have a question regarding how it (and DBI underneath) handle Date objects.
I use SQL Server 2016.
I have a query that drops rows when a refresh is required. Something like this:
library(dbplyr)
library(glue)
library(rlang)
test_con <- simulate_mssql()
first_new_date <- as.Date("2014-01-01")
target_table <- sym("my_table")
sql_drop <- "
DELETE
FROM {target_table}
WHERE {target_table}.end_date >= {first_new_date}
"
# I get this
glue_sql(
sql_drop,
target_table = target_table,
first_new_date = first_new_date,
.con = test_con
)
#> <SQL> DELETE
#> FROM my_table
#> WHERE my_table.end_date >= 2014-01-01
# I want this
glue_sql(
sql_drop,
target_table = target_table,
first_new_date = as.character(first_new_date),
.con = test_con
)
#> <SQL> DELETE
#> FROM my_table
#> WHERE my_table.end_date >= '2014-01-01'Created on 2018-07-19 by the reprex package (v0.2.0).
As you can see, the Date is returned without quotes. Is this ever useful? I don't know a ton about every other flavor of SQL, but I don't know of any that can use unquoted dates.
Regarding DBI, I was going to suggest that you could use dbQuoteLiteral() rather than dbQuoteString() in glue:::sql_quote_transformer(), but it seems to try and print the time zone for Date objects when it coerces them to character. This doesn't work with SQL Server, and I'm not sure where it would apply.
DBI::dbQuoteLiteral(test_con, first_new_date)
#> <SQL> '2014-01-01 UTC'Is the best solution just to coerce to character first, then run glue_sql()? Or is it worth looking into further? Thanks!