This could be due to a mismatch between your machine's local time zone (Sys.timezone()) and the timezone of the data you are retrieving.
RODBC uses as.POSIXct which uses your machine's current time zone by default. If the data contains any time stamps invalid or ambiguous in that timezone weirdstuff might happen. For example transitions to and from daylight savings may cause trouble.
These work fine because both time stamps are well defined in the local
timezone and in UTC:
> as.POSIXct(c("2016-01-01 00:00:01","2016-03-26 02:30:00"))
[1] "2016-01-01 00:00:01 CET" "2016-03-26 02:30:00 CET"
> as.POSIXct(c("2016-01-01 00:00:01","2016-03-26 02:30:00"), tz="UTC")
[1] "2016-01-01 00:00:01 UTC" "2016-03-26 02:30:00 UTC"
The first of these silently drops H:M:S on all timestamps because
the last time stamp is invalid in the local time zone:
> as.POSIXct(c("2016-01-01 00:00:01","2016-03-27 02:30:00"))
[1] "2016-01-01 CET" "2016-03-27 CET"
> as.POSIXct(c("2016-01-01 00:00:01","2016-03-27 02:30:00"), tz="UTC")
[1] "2016-01-01 00:00:01 UTC" "2016-03-27 02:30:00 UTC"
One solution is to fetch data with as.is, then manually converting to POSIXct specifying the proper time zone. In this case the db contains UTC time stamps:
data <- sqlQuery(mssql,
"SELECT timestamp, value FROM table",
as.is=c(TRUE, FALSE))
data$timestamp <- as.POSIXct(data$timestamp, tz="UTC")