ROracle - dbWriteTable, date columns

2019-05-11 09:04发布

How can I write data frame with date columns to Oracle database using dbWriteTable? I am trying:

df <-read.table(header = TRUE, sep = ',', stringsAsFactors = FALSE,
text="
id,product,origination_date,repayment_date
1,A,2012-01-01,2012-02-01
2,A,2012-01-01,2012-04-01
3,A,2012-01-01,NA
4,A,2012-02-01,2012-03-01
")

df$origination_date <- as.Date(df$origination_date, format = '%Y-%d-%m')
df$repayment_date <- as.Date(df$repayment_date, format = '%Y-%d-%m')
names(df) <- toupper(names(df))

dbWriteTable(oraCon, 'TMP', df)

This is result in database:

1   A   31-DEC-11 11.00.00.000000 PM    01-JAN-12 11.00.00.000000 PM
2   A   31-DEC-11 11.00.00.000000 PM    03-JAN-12 11.00.00.000000 PM
3   A   31-DEC-11 11.00.00.000000 PM    
4   A   01-JAN-12 11.00.00.000000 PM    02-JAN-12 11.00.00.000000 PM

I can maybe understand that I end up with timestamp and not date, but cannot understand why there is 31-DEC-11 where I wanted 01-JAN-12.

标签: r oracle roracle
1条回答
唯我独甜
2楼-- · 2019-05-11 09:56

This problem is due to the timezone settings of both your operating system and your Oracle session. You can change these settings as follows:

Sys.setenv(TZ='CET')
Sys.setenv(ORA_SDTZ='CET')

Obviously you may have to choose a different timezone. I include these settings in the beginning of all my R scripts, but there may be a way to make these settings your new default.

See here for more information on the Oracle timezone parameters.

查看更多
登录 后发表回答