简体   繁体   English

SAP HANA - 处理日期数据类型的问题

[英]SAP HANA - problem with handling date-datatype

I am using SAP HANA and I need to do some things with dates (add days mainly).我正在使用 SAP HANA,我需要对日期做一些事情(主要是添加日期)。

I am having a problem working with a particular day of the year, the 2nd Saturday of May.我在一年中的某个特定日子(5 月的第二个星期六)工作时遇到问题。 My guess is that this is related to daylight saving.我的猜测是这与夏令时有关。

I simplify the query to show the problem.我简化查询以显示问题。

DO
BEGIN
    DECLARE FECHA VARCHAR(20) = '2020-05-10';

    SELECT :FECHA AS D1
         , TO_DATE(:FECHA) AS D2
         , TO_VARCHAR(TO_DATE(:FECHA)) AS D3
      FROM DUMMY;
END;

What I get as a result is this:我得到的结果是:

|------------|-------------|------------------------|------------|
|    D1      |      D2     |           D3           |     D4     |
|------------|-------------|------------------------|------------|
| 2020-05-10 | 09-may-2020 | 09-may-2020 23:00:00.0 | 2020-05-10 |
|------------|-------------|------------------------|------------|

This causes all sorts of problems when adding days which is what I am encountering.添加天数时,这会导致各种问题,这就是我遇到的问题。 But just this simple example shows more than enough.但仅仅这个简单的例子就足够了。

Thanks in advance for your suggestions.提前感谢您的建议。

Mikel Rychliski is nearly correct with his answer. Mikel Rychliski 的回答几乎是正确的。

As I explained in the blog post Trouble with time?正如我在博客文章中解释的时间问题? a few years ago, SAP HANA Studio is a JAVA application that uses java.sql.Date / java.sql.Timestamp objects to handle date/timestamp information. a few years ago, SAP HANA Studio is a JAVA application that uses java.sql.Date / java.sql.Timestamp objects to handle date/timestamp information. And to use those, a Calendar has to be configured.要使用这些,必须配置Calendar

Without further configuration (ie the mentioned JVM parameter -Duser.timezone ) the JVM uses its default timezone setting.无需进一步配置(即提到的 JVM 参数-Duser.timezone ) JVM 使用其默认时区设置。

To quote from my post:引用我的帖子:

If the JVM runs on Linux, that would be the value of TZ again, on MS Windows it would be the current timezone setting for the Windows user that runs the JVM. If the JVM runs on Linux, that would be the value of TZ again, on MS Windows it would be the current timezone setting for the Windows user that runs the JVM.

If you want to convince yourself that the data in SAP HANA is correct, using a non-JDBC based connection is an easy way to do so.如果您想说服自己 SAP HANA 中的数据是正确的,那么使用基于非 JDBC 的连接是一种简单的方法。

For example, just run your code via hdbsql or an ODBC client.例如,只需通过hdbsql或 ODBC 客户端运行您的代码。

I think the problem is with Hana Studio since in DBeave it works as expected:PI am leaving this question here in case someone else needs to know how to fix this.我认为问题出在 Hana Studio 上,因为在 DBeave 中它按预期工作:PI 将这个问题留在这里,以防其他人需要知道如何解决这个问题。

I will try to find a way to fix it in Hana Studio and post back.我会尝试在 Hana Studio 中找到一种方法来修复它并回发。

HANA Studio (and all other JDBC applications) assume that DATE 's stored in the database are UTC. HANA Studio(和所有其他 JDBC 应用程序)假设DATE存储在数据库中是 UTC。 These values are converted to the local timezone of the client when they are fetched.这些值在获取时会转换为客户端的本地时区。

In your own JDBC application, you could pass a Calendar object on the getDate() call to specify which timezone you would like the server value converted to.在您自己的 JDBC 应用程序中,您可以在 getDate() 调用中传递日历 object 以指定您希望将服务器值转换为哪个时区。

HANA Studio depends on the JVM timezone setting, which by default is read from the OS. HANA Studio 依赖于 JVM 时区设置,默认情况下从操作系统读取。 To avoid this issue in HANA Studio you can add the JVM property -Duser.timezone=UTC in the hdbstudio.ini file (or change the timezone on you machine).为避免在 HANA Studio 中出现此问题,您可以在 hdbstudio.ini 文件中添加 JVM 属性-Duser.timezone=UTC (或更改您机器上的时区)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM