설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 94건, 최근 0 건
   

날짜 연산

글쓴이 : PostgresDBA 날짜 : 2012-12-12 (수) 09:39 조회 : 8651
PostgreSQL 에서 날짜의 덧셈 뺄셈은 다음과 같이 수행할수 있습니다.
오라클에서의 syntax 와 동일하네요.

scott@[local]:5432 scottdb#SQL> select hiredate, 
hiredate - interval '2 day' ,
hiredate + interval '2 day' ,
hiredate - interval '2 month' ,
hiredate - interval '2 year
from emp where deptno=20;
  hiredate  |      ?column?       |      ?column?       |      ?column?       |      ?column?       
------------+---------------------+---------------------+---------------------+---------------------
 1980-12-17 | 1980-12-15 00:00:00 | 1980-12-19 00:00:00 | 1980-10-17 00:00:00 | 1978-12-17 00:00:00
 1981-04-02 | 1981-03-31 00:00:00 | 1981-04-04 00:00:00 | 1981-02-02 00:00:00 | 1979-04-02 00:00:00
 1982-12-09 | 1982-12-07 00:00:00 | 1982-12-11 00:00:00 | 1982-10-09 00:00:00 | 1980-12-09 00:00:00
 1983-01-12 | 1983-01-10 00:00:00 | 1983-01-14 00:00:00 | 1982-11-12 00:00:00 | 1981-01-12 00:00:00
 1981-12-03 | 1981-12-01 00:00:00 | 1981-12-05 00:00:00 | 1981-10-03 00:00:00 | 1979-12-03 00:00:00
(5 rows)

Time: 1.345 ms
scott@[local]:5432 scottdb#SQL> 

이번에는 날짜 차를 구해볼까요?
scott@[local]:5432 scottdb#SQL> select smith_hiredate, allen_hiredate, 
allen_hiredate - smith_hiredate 
from (
select hiredate as smith_hiredate 
from emp
where ename='SMITH'
) x,
(
select hiredate as allen_hiredate
from emp
 where ename='ALLEN'
 ) y;
 smith_hiredate | allen_hiredate | ?column? 
----------------+----------------+----------
 1980-12-17     | 1981-02-20     |       65
(1 row)

Time: 2.618 ms
scott@[local]:5432 scottdb#SQL> 

   

postgresdba.com