extract date from timestamp in postgreSQL -


we have table of data looks like:

|id|date1     |timestamp1         | ----------------------------- |1 |2015-06-23|2015-06-23 16:02:00| ----------------------------- |2 |2015-01-02|2015-01-02 11:32:00| 

i tried create function , trigger not working, looks this:

create or replace function insert_date1_trg_func() returns trigger $body$ begin update schema.table set date1 = extract(date new.timestamp1) id = new.id; return null; end; $body$ language plpgsql volatile cost 100; 

trigger

create trigger insert_date1_trg_func() after insert or update on schema.table each row execute procedure insert_date1_trg_func(); 

i getting error type date expression double precision.

if want set "date1" in update trigger, should this:

create or replace function insert_date1_trg_func() returns trigger $body$ begin   new.date1 = date_trunc('day', new.timestamp1)::date;   return new; end; $body$ language plpgsql stable; 

trigger:

create trigger insert_date1_trg_func() before insert or update on schema.table each row execute procedure insert_date1_trg_func(); 

note trigger should fire before insert or update or changes not persist in database.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -