Sql加減日、月、年

 

對日期加減日、月、年。例如,根據員工CLARK的HIREDATE(聘用日期),計算另外6個不同的日期: 聘用CLARK之前及之後的5天;聘用CLARK之前及之後的5個月;聘用CLARK之前及之後的5年。例如,聘用CLARK的日期爲“09-JUN- 1981”,要求返回如下結果集:

HD_MINUS_5D    HD_PLUS_5D       HD_MINUS_5M    HD_PLUS_5M      HD_MINUS_5Y    HD_PLUS_5Y

-----------       -----------       -----------       -----------       -----------       -----------

04-JUN-1981  14-JUN-1981  09-JAN-1981  09-NOV-1981 09-JUN-1976  09-JUN-1986

12-NOV-1981 22-NOV-1981 17-JUN-1981  17-APR-1982 17-NOV-1976 17-NOV-1986

18-JAN-1982  28-JAN-1982  23-AUG-1981 23-JUN-1982  23-JAN-1977  23-JAN-1987

解決方案

DB2

對日期值,允許進行標準的加、減操作,但是,如果對日期進行加減操作,後面一定要給出它所表示的時間單位:

1 select hiredate -5 day       as hd_minus_5D,

2        hiredate +5 day        as hd_plus_5D,

3        hiredate -5 month       as hd_minus_5M,

4        hiredate +5 month       as hd_plus_5M,

5        hiredate -5 year  as hd_minus_5Y,

6        hiredate +5 year         as hd_plus_5Y

7   from emp

8  where deptno = 10

Oracle

對天數採用標準加減,而使用ADD_MONTHS 函數加減月數和年數:

1 select hiredate-5                 as hd_minus_5D,

2        hiredate+5                    as hd_plus_5D,

3        add_months(hiredate,-5)          as hd_minus_5M,

4        add_months(hiredate,5)          as hd_plus_5M,

5        add_months(hiredate,-5*12)       as hd_minus_5Y,

6        add_months(hiredate,5*12)       as hd_plus_5Y

7   from emp

8  where deptno = 10

PostgreSQL

同時使用標準加減與INTERVAL關鍵字,INTERVAL指定時間單位。在指定INTERVAL值時,需要用單引號:

1 select hiredate - interval '5 day'       as hd_minus_5D,

2        hiredate + interval '5 day'         as hd_plus_5D,

3        hiredate - interval '5 month'        as hd_minus_5M,

4        hiredate + interval '5 month'       as hd_plus_5M,

5        hiredate - interval '5 year'  as hd_minus_5Y,

6        hiredate + interval '5 year'         as hd_plus_5Y

7   from emp

8  where deptno=10

MySQL

同時使用標準加減與INTERVAL關鍵字,INTERVAL指定時間單位。它不同於PostgreSQL解決方案,不必在INTERVAL值周圍加單引號:

1 select hiredate - interval 5 day               as hd_minus_5D,

2        hiredate + interval 5 day                 as hd_plus_5D,

3        hiredate - interval 5 month         as hd_minus_5M,

4        hiredate + interval 5 month               as hd_plus_5M,

5        hiredate - interval 5 year           as hd_minus_5Y,

6        hiredate + interval 5 year          as hd_plus_5Y

7   from emp

8  where deptno=10

另外,也可以使用DATE_ADD函數,如下所示:

1 select date_add(hiredate,interval -5 day)        as hd_minus_5D,

2        date_add(hiredate,interval  5 day)          as hd_plus_5D,

3        date_add(hiredate,interval -5 month) as hd_minus_5M,

4        date_add(hiredate,interval  5 month)       as hd_plus_5M,

5        date_add(hiredate,interval -5 year)    as hd_minus_5Y,

6        date_add(hiredate,interval  5 year)  as hd_plus_5DY

7  from emp

8  where deptno=10

SQL Server

使用DATEADD函數,可以對日期進行不同時間單位的加減操作:

1 select dateadd(day,-5,hiredate)        as hd_minus_5D,

2        dateadd(day,5,hiredate)                  as hd_plus_5D,

3        dateadd(month,-5,hiredate)         as hd_minus_5M,

4        dateadd(month,5,hiredate)                as hd_plus_5M,

5        dateadd(year,-5,hiredate)           as hd_minus_5Y,

6        dateadd(year,5,hiredate)            as hd_plus_5Y

7   from emp

8  where deptno = 10

討論

當進行日期運算時,Oracle解決方案採用整型值表示天數。然而,這種方法只能用來對DATE類型進行運 算。Oracle9i Database引入了TIMESTAMP類型,對這種類型的值應該使用PostgreSQL 給出的INTERVAL解決方案;還要注意,不要把TIMESTAMP傳遞給老版本的日期函數,如ADD_MONTHS,因爲這樣做會丟失 TIMESTAMP值可能包含的小數部分。

INTERVAL 關鍵字以及與它一起使用的字符串符合ISO標準的SQL語法,該標準要求間隔時間單位用單引號括起來。PostgreSQL(以及Oracle9i Database,或更高版本)遵從此標準。但由於MySQL不支持引號,因此在某種程度上它沒有遵從此標準。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章