1. 问题: 
  2. 对日期加减日、月、年。例如,根据员工CLARK的HIREDATE(聘用日期),计算另外6个不同的日期: 聘用CLARK之前及之后的5天;聘用CLARK之前及之后的5个月;聘用CLARK之前及之后的5年。例如,聘用CLARK的日期为“09-JUN- 1981”,要求返回如下结果集: 
  3. HD_MINUS_5D    HD_PLUS_5D       HD_MINUS_5M    HD_PLUS_5M      HD_MINUS_5Y    HD_PLUS_5Y 
  4. -----------       -----------       -----------       -----------       -----------       ----------- 
  5. 04-JUN-1981  14-JUN-1981  09-JAN-1981  09-NOV-1981 09-JUN-1976  09-JUN-1986 
  6. 12-NOV-1981 22-NOV-1981 17-JUN-1981  17-APR-1982 17-NOV-1976 17-NOV-1986 
  7. 18-JAN-1982  28-JAN-1982  23-AUG-1981 23-JUN-1982  23-JAN-1977  23-JAN-1987 
  8. 解决方案 
  9. DB2对日期值,允许进行标准的加、减操作,但是,如果对日期进行加减操作,后面一定要给出它所表示的时间单位: 
  10.  select hiredate -5 day       as hd_minus_5D, 
  11.         hiredate +5 day        as hd_plus_5D, 
  12.         hiredate -5 month       as hd_minus_5M, 
  13.         hiredate +5 month       as hd_plus_5M, 
  14.         hiredate -5 year  as hd_minus_5Y, 
  15.         hiredate +5 year         as hd_plus_5Y 
  16.    from emp 
  17.   where deptno = 10 
  18. Oracle对天数采用标准加减,而使用ADD_MONTHS 函数加减月数和年数: 
  19.  select hiredate-5                 as hd_minus_5D, 
  20.         hiredate+5                    as hd_plus_5D, 
  21.         add_months(hiredate,-5)          as hd_minus_5M, 
  22.         add_months(hiredate,5)          as hd_plus_5M, 
  23.         add_months(hiredate,-5*12)       as hd_minus_5Y, 
  24.         add_months(hiredate,5*12)       as hd_plus_5Y 
  25.    from emp 
  26.   where deptno = 10 
  27. PostgreSQL同时使用标准加减与INTERVAL关键字,INTERVAL指定时间单位。在指定INTERVAL值时,需要用单引号: 
  28.  select hiredate - interval '5 day'       as hd_minus_5D, 
  29.         hiredate + interval '5 day'         as hd_plus_5D, 
  30.         hiredate - interval '5 month'        as hd_minus_5M, 
  31.         hiredate + interval '5 month'       as hd_plus_5M, 
  32.         hiredate - interval '5 year'  as hd_minus_5Y, 
  33.         hiredate + interval '5 year'         as hd_plus_5Y 
  34.    from emp 
  35.   where deptno=10 
  36.  
  37. MySQL同时使用标准加减与INTERVAL关键字,INTERVAL指定时间单位。它不同于PostgreSQL解决方案,不必在INTERVAL值周围加单引号: 
  38.  select hiredate - interval 5 day               as hd_minus_5D, 
  39.         hiredate + interval 5 day                 as hd_plus_5D, 
  40.         hiredate - interval 5 month         as hd_minus_5M, 
  41.         hiredate + interval 5 month               as hd_plus_5M, 
  42.         hiredate - interval 5 year           as hd_minus_5Y, 
  43.         hiredate + interval 5 year          as hd_plus_5Y 
  44.    from emp 
  45.   where deptno=10 
  46. 另外,也可以使用DATE_ADD函数,如下所示: 
  47.  select date_add(hiredate,interval -5 day)        as hd_minus_5D, 
  48.         date_add(hiredate,interval  5 day)          as hd_plus_5D, 
  49.         date_add(hiredate,interval -5 monthas hd_minus_5M, 
  50.         date_add(hiredate,interval  5 month)       as hd_plus_5M, 
  51.         date_add(hiredate,interval -5 year)    as hd_minus_5Y, 
  52.         date_add(hiredate,interval  5 year)  as hd_plus_5DY 
  53.   from emp 
  54.   where deptno=10 
  55. SQL Server使用DATEADD函数,可以对日期进行不同时间单位的加减操作: 
  56.  select dateadd(day,-5,hiredate)        as hd_minus_5D, 
  57.         dateadd(day,5,hiredate)                  as hd_plus_5D, 
  58.         dateadd(month,-5,hiredate)         as hd_minus_5M, 
  59.         dateadd(month,5,hiredate)                as hd_plus_5M, 
  60.         dateadd(year,-5,hiredate)           as hd_minus_5Y, 
  61.         dateadd(year,5,hiredate)            as hd_plus_5Y 
  62.    from emp 
  63.   where deptno = 10 
  64. 讨论 
  65. 当进行日期运算时,Oracle解决方案采用整型值表示天数。然而,这种方法只能用来对DATE类型进行运 算。Oracle9i Database引入了TIMESTAMP类型,对这种类型的值应该使用PostgreSQL 给出的INTERVAL解决方案;还要注意,不要把TIMESTAMP传递给老版本的日期函数,如ADD_MONTHS,因为这样做会丢失 TIMESTAMP值可能包含的小数部分。 
  66.  
  67. INTERVAL 关键字以及与它一起使用的字符串符合ISO标准的SQL语法,该标准要求间隔时间单位用单引号括起来。PostgreSQL(以及Oracle9i Database,或更高版本)遵从此标准。但由于MySQL不支持引号,因此在某种程度上它没有遵从此标准。