'Binding parameterized query failing when SQL invariant data format used
I'm receiving the following error when attempting to bind a parameterized query with dates in the SQL invariant format 'YYYYMMDD' (see full example below):
# Error in result_bind(res@ptr, params, batch_rows): nanodbc/nanodbc.cpp:1655: 22018:
# [Microsoft][ODBC Driver 13 for SQL Server]Invalid character value for cast specification
The error thrown seems to indicate an error casting the strings into date format in nanodbc. I've reviewed the nanodbc documentation, but couldn't find any reference to permitted date formats.
These parameters are bound successfully when an alternate date format is used ('YYYY-MM-DD'):
library(DBI)
my_conn <- dbConnect(odbc::odbc(), driver = "ODBC Driver 13 for SQL Server",
database = db, server = server)
query <- "SELECT myvar FROM dbo.myTable WHERE myvar BETWEEN ? AND ?"
interim_res <- DBI::dbSendQuery(my_conn, query)
dbBind(interim_res, list("20210101", "20211231") # does not work, throws error below
# Error in result_bind(res@ptr, params, batch_rows): nanodbc/nanodbc.cpp:1655: 22018:
# [Microsoft][ODBC Driver 13 for SQL Server]Invalid character value for cast specification
dbBind(interim_res, list("2021-01-01", "2021-12-31") # this date format works
Could someone please help me understand why dates in the format 'YYYYMMDD' fail to be bound?
TIA!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
