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


총 게시물 94건, 최근 0 건
   

현재월의 첫날과 마지막 날짜 구하기

글쓴이 : PostgresDBA 날짜 : 2012-12-20 (목) 21:56 조회 : 16294
오라클에서는 아래 구문으로 시작일, 마지막일을 쉽게 구할수 있습니다.
특히 마지막일은 LAST_DAY 함수를 이용하면 쉽습니다.

SCOTT@ORA11GR2>select trunc(sysdate,'MM'), last_day(sysdate) from dual;
TRUNC(SYSDAT LAST_DAY(SYS
------------ ------------
01-DEC-12    31-DEC-12

SCOTT@ORA11GR2>

아쉽게도 PostgreSQL LAST_DAY 함수는 없습니다.

대신 아래와 같이 구현할수 있습니다.
자주 쓰인다면 아래를 응용하여 함수를 만들면 되겠네요^^

scott@pg-00:5432:scottdb] 
SQL>   SELECT cast(date_trunc('month',current_date) as date) as firstday, (date_trunc('MONTH', current_date) + INTERVAL '1 MONTH - 1 day')::date lastday;
  firstday  |  lastday   
------------+------------
 2012-12-01 | 2012-12-31
(1 row)


   

postgresdba.com