dbGetQuery returns date that differs by an hour

假装没事ソ 提交于 2019-12-25 13:38:10

问题


In R I am running the following query to retrieve data:

test <- dbGetQuery(conn = GetConnection("default"), statement = "SELECT  PK_FK_RW_BOND_HOLDING_VAL_BOND, PK_CASHFLOW_DAT FROM RW_CASH_FLOWS_ON_BONDS WHERE PK_FK_RW_BOND_HOLDING_VAL_BOND = 'AT0000385745'
                   OR PK_FK_RW_BOND_HOLDING_VAL_BOND = 'RU000A0JV7J9'")

This returns

PK_FK_RW_BOND_HOLDING_VAL_BOND  PK_CASHFLOW_DAT
RU000A0JV7J9                    2018-01-14 23:00:00
RU000A0JV7J9                    2017-01-14 23:00:00
RU000A0JV7J9                    2019-08-01 00:00:00
RU000A0JV7J9                    2019-01-31 23:00:00
RU000A0JV7J9                    2018-08-01 00:00:00
RU000A0JV7J9                    2018-01-31 23:00:00
AT0000385745                    2017-08-01 00:00:00
AT0000385745                    2017-01-31 23:00:00

where the PK_CASHFLOW_DAT is of the class ("POSIXct" "POSIXt"). On the other hand, if I run the exact same query in SQL (Toad for Oracle) it returns

PK_FK_RW_BOND_HOLDING_VAL_BOND  PK_CASHFLOW_DAT
RU000A0JV7J9                    1-8-2019
RU000A0JV7J9                    1-2-2019
RU000A0JV7J9                    1-8-2018
RU000A0JV7J9                    1-2-2018
RU000A0JV7J9                    1-8-2017
RU000A0JV7J9                    1-2-2017
AT0000385745                    15-1-2018
AT0000385745                    15-1-2017

Now, the DateType of PK_CASHFLOW_DAT is DATE. The timezone of my computer as well as that of the server are UK.

My question is twofold:
1. Why is this happening in the first place.
2. I actually need the date to be in a character format. Is there some easy way in which we request the DateType Date to be received as character in R, instead of ("POSIXct" "POSIXt")?

Thanks!


回答1:


This problem is due to the default time zone settings of your operating system and your oracle session. You can overwrite these settings. In my case the following specification does the trick.

Sys.setenv(TZ='CET')        # Operating system
Sys.setenv(ORA_SDTZ='CET')  # Oracle Session

I include these settings in the beginning of all my R scripts, but possibly there is also a way to make these settings your new default.

Here is more information on the Oracle time zone parameter: Setting the Session Time Zone



来源:https://stackoverflow.com/questions/44265617/dbgetquery-returns-date-that-differs-by-an-hour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!