H2 数据库字符串到时间戳 [英] H2 database string to timestamp

查看:29
本文介绍了H2 数据库字符串到时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我必须插入像17-09-2012 18:47:52.69"这样的数据.函数 PARSEDATETIME 减少毫秒.查询示例:

Hello, I have to insert data like '17-09-2012 18:47:52.69'. Function PARSEDATETIME cuts milliseconds. Query example:

CREATE TABLE TEST(ID NUMBER(19) not null,
DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

INSERT INTO TEST (ID, DATE) VALUES(1,
parsedatetime('17-09-2012 18:47:52.69', 'dd-MM-yyyy hh:mm:ss.SS'))

在 SELECT 之后,我看到毫秒为零.

After SELECT I see that milliseconds are zeroes.

什么是解决方案?

推荐答案

根据我的测试,H2 1.3.170版本,毫秒实际上不是0,而是069:

According to my test, with H2 version 1.3.170, the milliseconds are not actually zero, but 069:

select * from test;
ID  DATE  
1   2012-09-17 18:47:52.069

如果你运行,也会发生同样的情况:

The same happens if you run:

call parsedatetime('17-09-2012 18:47:52.69', 'dd-MM-yyyy hh:mm:ss.SS');

如果你添加一个零,那么它会起作用:

If you add a zero then it works:

call parsedatetime('17-09-2012 18:47:52.690', 'dd-MM-yyyy hh:mm:ss.SS');

H2 内部使用 java.text.SimpleDateFormat,因此它必须承受同样的限制.如果你在 SimpleDateFormat 中找到了解决方案,你可以在 H2 的 parsedatetime 函数中使用它.

H2 internally uses java.text.SimpleDateFormat, so it has to live with the same limitations. If you find a solution within SimpleDateFormat, you can use it within the parsedatetime function in H2.

另一种方法是使用 JDBC 中定义的 ISO 时间戳格式.这应该适用于所有符合 JDBC 标准的数据库:

An alternative is to use the ISO timestamp format as defined in JDBC. This is supposed to work with all databases that conform the JDBC standard:

INSERT INTO TEST VALUES(2, {ts '2012-09-17 18:47:52.69'});

这篇关于H2 数据库字符串到时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆