【发布时间】:2020-07-25 04:32:18
【问题描述】:
为了使我的主要代码更具可读性,我在函数中封装了一个将日期转换为纪元格式的操作。但是,该函数返回不正确的值。我做错了什么?
我得到了什么
功能
create or replace function date_to_epoch(
date_in in date)
return number
is
epoch_out number;
begin
epoch_out := round((to_date(date_in, 'yyyy-mm-dd hh24:mi:ss') - to_date('1970-01-01', 'yyyy-mm-dd'))*24*60*60);
return epoch_out;
end;
我调用函数:
declare
date_out number;
begin
select date_to_epoch(to_date('2020-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')) into date_out from dual;
DBMS_OUTPUT.PUT_LINE('Output:' || date_out);
end;
返回:-62134128000
我的期望
当我在一个简单的 SQL 语句中运行相同的操作时,我得到了预期的输出:
select round((to_date('2020-01-01', 'yyyy-mm-dd hh24:mi:ss') - to_date('1970-01-01', 'yyyy-mm-dd'))*24*60*60) from dual;
返回:1577836800
【问题讨论】:
标签: sql oracle date plsql sql-function