tover
tover

Reputation: 545

posix written through rodbc into mysql database gets truncated

When I write a column of class POSIXct into a MySQL database using RODBC it gets truncated to only the year. This happens for example with:

sqlSave(connection, dat = data.frame(date = as.POSIXct("2015-01-01 08:10:00")+0:10*60), 
    tablename = "date_column")

How can I avoid this?

Upvotes: 0

Views: 215

Answers (1)

lukeA
lukeA

Reputation: 54237

You probably need to make the column of type datetime in mysql explicitly like this:

library(RODBC)
con <- odbcConnect("mysql", uid="root", case = "tolower")
(dat <- data.frame(date = Sys.time()))
#                  date
# 1 2015-03-08 23:55:33
res <- sqlSave(con, dat = dat, varTypes = c("date" = "datetime"), tablename = "date_column")
close(con)

Upvotes: 1

Related Questions