加减年、月、日、时、分、秒
创建测试用表:
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;
在Oracle中,date类型可以直接加减天数,而加减月份要用add_months函数。
SQL代码如下:
SELECT c AS 原日期, c - 5 AS 减5天, c + 5 AS 加5天, ADD_MONTHS(c, -5) AS 减5个月, ADD_MONTHS(c, 5) AS 加5个月, ADD_MONTHS(c, -5 * 12) AS 减5年, ADD_MONTHS(c, 5 * 12) AS 加5年, c - 5 / 60 / 60 / 24 AS 减5秒, c + 5 / 60 / 60 / 24 AS 加5秒, c - 5 / 60 / 24 AS 减5分钟, c + 5 / 60 / 24 AS 加5分钟, c - 5 / 24 AS 减5小时, c + 5 / 24 AS 加5小时 FROM v;
执行结果如下: