Error changing table in Postgresql. How do I change the type of an int column to the timestamp type?

Asked

Viewed 99 times

3

I’m studying the Postgresql comic book for a recent book I acquired. When trying to change a tebela based on the command I learned in the book an error occurred, I used exactly the same command written in the book without change. I am trying to change an int type field to timestamp type but the following error occurs

--table change with int column change to timestamp type

alter table commissions alter column data_payment type timestamp using data_payment_timestamp;

Obs: in the book is written time_payment_timestamp. Timestamp is a type in SQL to store date and time however with "timestamp" stands as part of the table name. I removed the "" and I left a separate timestamp from the column name because I thought it was a book error but it didn’t work because it still doesn’t work. How to fix this?

the error occurring below:

LINE 1: ...ata_payment type timestamp using data_payment timestamp; ^ SQL state: 42601 Character: 87

1 answer

0

timestamp es una fecha con una hora.

To transform numbers into timestamp Hay that indicate these numbers which represent (seconds, minutes, hours). Y to which corresponds el 0.

Per ejemplo si son segundos y Hoy es el momento 0:

create table table11(
  id serial primary key,
  seconds int
);

insert into table11 (seconds) values 
  (15), (60), (60*60);

alter table table11 alter seconds type timestamp
  using '2018-11-18'::date + (seconds||' seconds')::interval;

select * from table11;  

Note that the part USING is the one that indicates how the transformation should be done.

To see an example, go to: https://dbfiddle.uk/? rdbms=postgres_11&fiddle=9f687c66e2fa82bddd14130d82c27b3a

Browser other questions tagged

You are not signed in. Login or sign up in order to post.