Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

dbReadTable coercing date column from SQL database to character

$
0
0

I can write a date to an SQL table with DBI::dbWriteTable() and see that the column is in fact formated as a date on the database. But when I use DBI::dbReadTable() the same date column is coerced to a character column in R.

How do I read the SQL table into R and preserve the date column format?

library(DBI)

df <- data.frame(date = as.Date("2012-01-01"))
class(df$date)
#> [1] "Date"

udt <- dbConnect(odbc::odbc(), "udt")
dbWriteTable(udt, name = Id(schema = "FarmingAnalytics", table = "test"), value = df, overwrite = TRUE)

df2 <- dbReadTable(udt, name = Id(schema = "FarmingAnalytics", table = "test")) 
class(df2$date)
#> [1] "character"

Created on 2020-01-20 by the reprex package (v0.3.0)


Viewing all articles
Browse latest Browse all 201867

Trending Articles