The goal of this tutorial is to read properly dates and times from an RODBC connection. By default most of the drivers and connectors transform date-time variables into date (POSIXct) variables, therefore we lose the time information in these variables.
# First we load the libraries
# In this case we use the R Open Database Connectivity library
library(RODBC)
# We use the sqlQuery function to query the dates from the table
mydf <- sqlQuery(channel, "SELECT date FROM mytable")
head(mydf)
## [1] "2018-03-07" "2018-03-07" "2018-03-07" "2018-03-07" "2018-03-07"
## [6] "2018-03-07"
# In order to get the right datetime we need to use the parameter as.is, then the query will return characters and it is up to us to give the proper format to the variables
mydf <- sqlQuery(channel, "SELECT date FROM mytable", as.is = T)
# Now we have obtained the time information that we need in order to do our experiments
head(mydf)
## [1] "2018-03-07 08:52:48.432" "2018-03-07 08:53:00.939"
## [3] "2018-03-07 08:53:01.009" "2018-03-07 08:54:00.956"
## [5] "2018-03-07 08:55:00.859" "2018-03-07 08:55:00.916"
In this tutorial we have learnt how to obtain the right datetime information from a RODBC connection.