Using R-3.5 and [
odbc
](https://github.com/r-dbi/odbc) (which uses [nanodbc
](https://github.com/nanodbc/nanodbc)) , I connect via ODBC and the ODBC Driver 13 for SQL Server
ODBC driver on windows 10.
DBI::dbGetQuery(odbccon, "select SYSDATETIMEOFFSET() as now")
# now
# 1 2018-09-03 16:36:44 Pacific Standard Time
The same query with a native (non-ODBC) driver:
DBI::dbGetQuery(nativecon, "select SYSDATETIMEOFFSET() as now")
# now
# 1 2018-09-03 19:37:13.9923643 -04:00
(Both are R's character
type.)
This demonstrates a few things:
- the ODBC driver does not recognize an object of type DATETIMEOFFSET
, which an [issue I've raised with odbc
](https://github.com/r-dbi/odbc/issues/207) ; this is not of itself a huge issue, but I think indicates why it is returned as a string (and not internally converted correctly, I think)
- loss of millisecond precision (the biggest problem)
- non-standard time zone indication (where I believe "standard" includes -07:00
, -0700
, +07
, or even America/Los_Angeles
)
Is there a way to prevent SQL Server or Windows ODBC from formatting (and losing) data thus? Is there a way to change this via a SQL Server command, an ODBC function, or something else? (The largest problem is omission of milliseconds.)
**Edit**: for fairness, having it come as a character
vice a native POSIXt
is not a big problem, that's easy enough to fix with a call to as.POSIXct
; similarly, I can compensate for the timezone depiction using a lookup on something built from [this "Zone -> Tzid" table](https://www.unicode.org/cldr/charts/latest/supplemental/zone_tzid.html) . It's the loss-of-data thing I really need to mitigate, since my immediate use-case uses milli-second precision, and altering all processes to explicitly cast(...)
is more than I'd like to do right now (and is not a general solution).
DBI::dbGetQuery(con, "select @@VERSION")
#
# 1 Microsoft SQL Server 2016 (RTM-GDR) (KB4019088) - 13.0.1742.0 (X64) \n\tJul 5 2017 23:41:17 \n\tCopyright (c) Microsoft Corporation\n\tStandard Edition (64-bit) on Windows Server 2016 Standard 6.3 (Build 14393: )\n
Asked by r2evans
(121 rep)
Sep 4, 2018, 12:01 AM
Last activity: Sep 4, 2018, 03:04 PM
Last activity: Sep 4, 2018, 03:04 PM