如何清理SQL表达式

时间:2019-05-09 16:51:54

标签: informix

下面的代码将recv.rcpt_dtim(这是一种日期时间类型)与当前日期/时间进行比较。它计算经过的时间,以小时和分钟为单位,格式为:“ 04:22”。我花了一些时间才能使它正常工作,但看起来似乎很草率。有人有任何清理技巧吗?

TRIM((((CURRENT YEAR TO SECOND - recv.rcpt_dtim)::INTERVAL SECOND(9) to 
SECOND)/3600)::VARCHAR(12) || ':' || CASE WHEN (MOD(MOD(((CURRENT YEAR TO 
MINUTE - recv.rcpt_dtim)::INTERVAL MINUTE(9) to 
MINUTE)::VARCHAR(12)::INT,60),60))<10 THEN "0" ELSE "" END || 
(MOD(MOD(((CURRENT YEAR TO MINUTE - recv.rcpt_dtim)::INTERVAL MINUTE(9) 
to MINUTE)::VARCHAR(12)::INT,60),60))::VARCHAR(12))

1 个答案:

答案 0 :(得分:1)

使用Informix 12.10.FC12开发人员版,我可以执行以下操作:

CREATE TABLE test_time
(
    rcpt_dtim DATETIME YEAR TO SECOND
);

INSERT INTO test_time VALUES ( '2019-05-09 10:01:01' );
INSERT INTO test_time VALUES ( '2019-05-09 10:01:59' );
INSERT INTO test_time VALUES ( '2019-05-09 13:01:00' );
INSERT INTO test_time VALUES ( '2019-05-09 15:01:00' );
INSERT INTO test_time VALUES ( '2019-04-02 22:01:00' );

SELECT
    ( CURRENT YEAR TO SECOND - rcpt_dtim )::INTERVAL HOUR(9) TO MINUTE AS elapsed
FROM
    test_time
;


elapsed
  12:47
  12:46
   9:47
   7:47
   0:47
 888:47