Oracle日期和时间时间差异

时间:2014-09-15 05:27:16

标签: sql oracle

我有一个名为RAM的表,它有四列,两个结束日期和时间以及两个开始日期和时间,我希望得到结束和开始之间的区别。我正在使用向下查询行,我收到无效数字的错误,任何想法如何纠正它?

A_STA_DT    A_STA_TIME  B_END_DT    B_END_TIME
6/19/1982   500 7/10/1982   500
7/10/1982   500 8/7/1982    500
8/31/1982   500 9/24/1982   500
9/24/1982   500 11/1/1982   0
5/5/1983    500 6/5/1983    500
7/4/1983    500 7/30/1983   500

SELECT 
TO_CHAR(TO_DATE ( TO_CHAR(RAM.B_END_DT, 'dd-mon-yyyy') || RAM.B_END_TIME, 'dd-mon-yyyy hh24:mi:ss'),'YYYY/MM/DD HH24:Mi')ENDDT,  
TO_CHAR(TO_DATE ( TO_CHAR(RAM.A_STA_DT, 'dd-mon-yyyy') || RAM.A_STA_TIME, 'dd-mon-yyyy hh24:mi:ss'),'YYYY/MM/DD HH24:Mi')STADT,
TRUNC(TO_CHAR(TO_DATE ( TO_CHAR(RAM.B_END_DT, 'dd-mon-yyyy') || RAM.B_END_TIME, 'dd-mon-yyyy hh24:mi:ss'),'YYYY/MM/DD HH24:Mi'))-TRUNC(TO_CHAR(TO_DATE ( TO_CHAR(RAM.A_STA_DT, 'dd-mon-yyyy') || RAM.A_STA_TIME, 'dd-mon-yyyy hh24:mi:ss'),'YYYY/MM/DD HH24:Mi'))
FROM RAM

1 个答案:

答案 0 :(得分:0)

当您连接日期和时间时,您不会添加空格|| ' ' ||。其次,当你减去日期时,删除trunc(to_char(。我认为以下将做你需要的。

SELECT 
    TO_DATE(TO_CHAR(RAM.B_END_DT, 'dd-mm-yyyy') || ' ' || to_char(RAM.B_END_TIME, '0999'), 'dd-mm-yyyy HH24Mi') ENDDT,
    TO_DATE(TO_CHAR(RAM.A_STA_DT, 'dd-mm-yyyy') || ' ' || to_char(RAM.A_STA_TIME, '0999'), 'dd-mm-yyyy HH24Mi') STADT,
    TO_DATE(TO_CHAR(RAM.B_END_DT, 'dd-mm-yyyy') || ' ' || to_char(RAM.B_END_TIME, '0999'), 'dd-mm-yyyy HH24Mi') -
    TO_DATE(TO_CHAR(RAM.A_STA_DT, 'dd-mm-yyyy') || ' ' || to_char(RAM.A_STA_TIME, '0999'), 'dd-mm-yyyy HH24Mi') Diftime
FROM 
    (select to_date('6/19/1982', 'mm/dd/yyyy') A_STA_DT, 500 A_STA_TIME,  to_date('7/10/1982', 'mm/dd/yyyy') B_END_DT, 600 B_END_TIME from dual) RAM