(Oracle PostgreSQL변환) PostgreSQL ERROR:42793: column “sysdate” dos not exist

공유하기

  • Add this entry to Hatena Bookmark
  • 0

Oracle과 달리 PostgreSQL에서는 sysdate가 쓰이지 않습니다. 대신에 now()함수를 사용해 현재 시간을 취득합니다. Oracle에서 개발한 SQL문을 PostgreSQL에서 그대로 이용하고 싶다면 이런 차이점을 파악해서 변경을 해야 합니다.

Oracle예

SQL> select to_char(sysdate, 'YYYY/MM/DD HH24:MI:SS') as "curdate" from dual;

curdate
-------------------
2021/06/05 17:44:49

PostgreSQL예

$ psql -U postgres
psql (13.3)
Type "help" for help.
postgres=# select to_char(now(), 'YYYY/MM/DD HH24:MI:SS') as "curdate";
       curdate
---------------------
 2021/06/05 09:02:44
(1 row)
postgres=#

또는 다음과 같이 sysdate()함수를 작성해서 사용할 수 있습니다.

CREATE OR REPLACE FUNCTION public.sysdate()
RETURNS TIMESTAMP WITHOUT TIME ZONE
AS
$BODY$
   SELECT clock_timestamp() AT TIME ZONE 'Asia/Seoul';
$BODY$ 
LANGUAGE sql;
$ psql -U postgres
psql (13.3)
Type "help" for help.
postgres=# select to_char(sysdate(), 'YYYY/MM/DD HH24:MI:SS') as "curdate" 
       curdate
---------------------
 2021/06/05 09:02:44
(1 row)