PostgreSQL/Date Timezone/now

Материал из SQL эксперт
Перейти к: навигация, поиск

Get current time for logging

postgres=#
postgres=# create table logtable(t varchar(10), value varchar(50));
CREATE TABLE
postgres=#
postgres=#
postgres=# CREATE FUNCTION logfunc2(logtxt text) RETURNS timestamp AS $$
postgres$#    DECLARE
postgres$#        curtime timestamp;
postgres$#    BEGIN
postgres$#        curtime := "now";
postgres$#        INSERT INTO logtable VALUES (logtxt, curtime);
postgres$#        RETURN curtime;
postgres$#    END;
postgres$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# select logfunc2("123");
         REATE
        logfunc2
------------------------
 2006-10-21 19:09:25.14
(1 row)
postgres=#
postgres=# select * from logtable;
            REATE
  t  |         value
-----+------------------------
 123 | 2006-10-21 19:09:25.14
(1 row)
postgres=#
postgres=#
postgres=# drop function logfunc2(logtxt text);
DROP FUNCTION
postgres=# drop table logtable;



now(): get current date and time

postgres=#
postgres=# SELECT current_timestamp, now() AS timestamp;
            now             |         timestamp
----------------------------+----------------------------
 2006-10-08 15:11:40.109-07 | 2006-10-08 15:11:40.109-07
(1 row)
postgres=#



SELECT now()

postgres=# SELECT now();
            now
----------------------------
 2006-10-08 15:16:29.968-07
(1 row)
postgres=#