I have a field1 with timestamp
, datatype
and values format is 2016-02-23 12:01:30
.
I'm running the query:
UPDATE <table> set field1 = '2015-12-31'::timestamp::date where .....
The output changes to:
2015-12-31 00:00:00
It converts the time to all zero's. How to change the date and retain the timestamp?
First we create a table that has both timestamp and timestamptz columns using the below command: CREATE TABLE timestamp_demo (ts TIMESTAMP, tstz TIMESTAMPTZ); Then we will set the time zone of database server to Asia/Calcutta as below: SET timezone = 'Asia/Calcutta';
Try this:
UPDATE mytable
SET field1 = '2015-12-31'::timestamp +
EXTRACT(HOUR FROM field1) * INTERVAL '1 HOUR' +
EXTRACT(MINUTE FROM field1) * INTERVAL '1 MINUTE' +
EXTRACT(SECOND FROM field1) * INTERVAL '1 SECOND'
WHERE ...
Demo here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With