H2数据库 - CSVREAD - 从csv读取时间戳到时间戳列

时间:2012-01-06 16:58:39

标签: java timestamp h2

我在Java应用程序中使用H2 DB。我想将.csv文件加载到表中。我的.csv文件包含格式为07/16/2011 02:43:43的时间戳,但H2 DB的默认时间戳格式为2005-12-31 23:59:59

所以当我使用CSVREAD将.csv文件加载到数据库时,我遇到以下异常:

org.h2.jdbc.JdbcSQLException: Cannot parse "TIMESTAMP" constant "07/16/2011 02:43:43"; SQL statement:
-- row #2 ('CIRCLE', 'MSISDN', 'PORT_IN_DATE', 'OPERATOR', 'PRODUCT_TYPE', 'PORT_ID') [22007-163]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:158)
at org.h2.value.ValueTimestamp.parse(ValueTimestamp.java:87)
at org.h2.value.Value.convertTo(Value.java:819)
at org.h2.table.Column.convert(Column.java:155)
at org.h2.command.dml.Insert.addRow(Insert.java:153)
at org.h2.command.dml.Select.queryFlat(Select.java:524)

1 个答案:

答案 0 :(得分:1)

select convert(parseDateTime(DOB,'yyyy/MM/dd hh:mm:ss'),timestamp) as NEWNAME
from CSVREAD('file.csv')

其中file.csv有一个标记为DOB的列

并插入你的表格:

insert into YOURTABLE 
    (
     select convert(parseDateTime(DOB,'yyyy/MM/dd hh:mm:ss'), timestamp)
     from CSVREAD('file.csv')
    )