创建测试用表:
CREATE OR REPLACE VIEW v AS SELECT TO_DATE('2015-1-1 13:14:15', 'YYYY-MM-DD HH24:MI:SS') AS c FROM DUAL; SELECT * FROM v;
两个date相减,得到的就是天数,乘以24就是小时,以此类推,可以计算出秒,如果要计算月份间隔,需要使用函数months_between。
SQL代码如下:
SELECT 间隔天数, 间隔天数 * 24 AS 间隔小时, 间隔天数 * 24 * 60 AS 间隔分, 间隔天数 * 24 * 60 * 60 AS 间隔秒, MONTHS_BETWEEN(SYSDATE, c) AS 间隔月, MONTHS_BETWEEN(SYSDATE, c) / 24 AS 间隔年 FROM (SELECT c, SYSDATE - c AS 间隔天数 FROM v);
执行结果如下: