我想比较两个时间值。第一个时间值是代表开始时间的自定义时间,例如,列名称为Business_Start_time,设置为6:00:00 am。我还想仅从Oracle的一列中提取时间,该列是一个看起来像'5/1/2019 12:57:19 PM'的日期字段,被称为'Completed_Date_Time'。这样做的目的是将业务开始日期与文件完成时间进行比较。我试图将“ Completed_Date_Time”字段转换为“ HH24:MI:SS”格式,这似乎将数据类型更改为char(8)值,不允许我比较两个时间戳。
CAST(TO_CHAR(Completed_Date_Time,'HH:MI:SS AM')AS CHAR(8))
答案 0 :(得分:0)
尽管您同时写了问题和评论,但我仍然不确定您拥有什么以及想要获得什么。示例案例会有所帮助(创建表并插入)。
与此同时,请注意以下几点:当减去两个DATE
数据类型值时,结果为天天,这意味着-如果要以以下格式显示它:更容易阅读和理解-您必须进行一些计算(一天有24小时;一个小时有60分钟),等等。
这是一个例子:
SQL> create table test
2 (business_Start_time date,
3 completed_date_Time date
4 );
Table created.
SQL> insert into test (business_start_time, completed_date_time) values
2 (to_date('05.01.2019 12:57:19', 'dd.mm.yyyy hh24:mi:ss'),
3 to_date('05.01.2019 18:58:20', 'dd.mm.yyyy hh24:mi:ss'));
1 row created.
简单地减去,就会得到
SQL> select completed_date_time - business_start_time result from test;
RESULT
----------
,250706019
SQL>
这是一个功能,它以另一种格式dd:hh:mi
(天:小时:分钟)(可以通过将第二个参数设置为0来省略天)来呈现这样的值:>
SQL> create or replace
2 function f_days2ddhhmi (par_broj_dana in number, par_cb_dd in number)
3 return varchar2
4 is
5 /* Converting number of days into dd:hh:mi format
6
7 Date from Date to Diff (days) Retval
8 -------------------- -------------------- -------------- ----------------------------------
9 20.11.2018. 07:00:00 - 20.11.2018. 13:45:00 0,28125 0:06:45 (6 hours 45 minutes)
10 23.10.2018. 07:00:00 - 25.10.2018. 22:12:00 2,63333 2:15:12 (2 daysa 15 hours 12 minutes)
11
12 PAR_BROJ_DANA: 0.28125
13 PAR_CB_DD : display number of days or not? 1 - yes --> 0:06:45
14 0 - no --> 06:45
15 */
16 l_broj_dana number := round (par_broj_dana, 15); -- to avoid 1.99999999999999 days = 1 day 24 hours
17 retval varchar2 (20);
18 begin
19 with podaci
20 as (select trunc (l_broj_dana) broj_dana,
21 round (mod (l_broj_dana * 24, 24), 2) broj_sati
22 from dual)
23 select decode (par_cb_dd,
24 1, lpad (p.broj_dana, 2, '0') || ':',
25 0, null)
26 || lpad (trunc (p.broj_sati), 2, '0')
27 || ':'
28 || lpad (round ( (p.broj_sati - trunc (p.broj_sati)) * 60),
29 2,
30 '0')
31 into retval
32 from podaci p;
33
34 return retval;
35 end f_days2ddhhmi;
36 /
Function created.
应用到test
表中,您将得到
SQL> select f_days2ddhhmi(completed_date_time - business_start_time, 0) result
2 from test;
RESULT
--------------------------------------------------------------------------------
06:01
这意味着相差6小时1分钟。
如果这是您的要求,请查看是否可以使用它。如有必要,可以随意将其增强到几秒钟。
答案 1 :(得分:0)
将值转换为TIMESTAMP
,然后您可以从截断到一天开始的值中减去这些值,以获得INTERVAL
,其中包含从午夜开始的时间,并且可以减去差值。
Oracle设置:
CREATE TABLE table_name ( Business_Start_time, Completed_Date_Time ) AS
SELECT '6:00:00 AM',
TO_DATE( '5/1/2019 12:57:19 PM', 'DD/MM/YYYY HH12:MI:SS AM' )
FROM DUAL
查询:
SELECT ( completed_time - TRUNC( completed_time ) ) -
( start_time - TRUNC( start_time ) ) AS time_difference
FROM (
SELECT TO_TIMESTAMP( business_start_time, 'HH12:MI:SS AM' ) AS start_time,
CAST( Completed_Date_Time AS TIMESTAMP ) AS completed_time
FROM table_name
)
输出:
| TIME_DIFFERENCE | | :---------------------------- | | +000000000 06:57:19.000000000 |
db <>提琴here