是否可以将 TIMESTAMP 字段转换为自纪元以来的毫秒数?
就像是:
select
toEpoch(current_timestamp)
from SYSIBM.SYSDUMMY1;
最佳答案
Apache Derby 没有 native 函数,但可以创建自己的函数并从数据库中调用它。
首先,创建将转换日期的 java 方法:
package DbExamples.StoredProcedures;
import java.sql.Timestamp;
public class DateUtilities {
public static long toEpoch(Timestamp inputDate) {
if (inputDate == null) {
return 0L;
}
Long result = inputDate.getTime();
return result;
}
}
然后通过在数据库上运行以下 sql 语句将 jar 文件注入(inject)数据库:
CALL SQLJ.REMOVE_JAR('App.StoredProcedures', 0);
CALL SQLJ.INSTALL_JAR('C:\dev\DbExamples\dist\DbExamples.jar', 'App.StoredProcedures', 0);
CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.database.classpath', 'App.StoredProcedures');
现在通过运行以下 sql 语句在数据库中创建存储过程:
drop function toEpoch;
create function toEpoch(inputDate timestamp)
returns bigint
parameter style java no sql
language java external name 'DbExamples.StoredProcedures.DateUtilities.toEpoch';
现在您可以运行查询:
select
toEpoch(current_timestamp)
from SYSIBM.SYSDUMMY1;
select
toEpoch(cast('2016-07-21 14:50:00' as timestamp))
from SYSIBM.SYSDUMMY1;
关于timestamp - 使用 Apache Derby 将日期转换为自纪元以来的毫秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38506549/