简体   繁体   中英

ALTER DATATYPE of Postgres table's column

I just started working on Postgres database and created few tables .For convenience initially, I used character varying(50) but now I want to change that to Date (dd/mm/yyyy) format.

I tried using this command

ALTER TABLE ip_role ALTER COLUMN event_dt TYPE date USING(event_dt::date);

also this-

ALTER TABLE ip_role ALTER COLUMN event_dt TYPE DATE using to_date(event_dt, 'DD/MM/YYYY');

but not getting the error as-

invalid input syntax for type date: "event_dt"

alter (changing datestyle to properly read dates):

t=# create table d2(t text);
CREATE TABLE
t=# insert into d2 select '2017-05-08';
INSERT 0 1
t=# set datestyle to YMD, ISO;
SET
t=# alter table d2 alter column t type date using t::date;
ALTER TABLE

you don't keep dates in some date format:

t=# select * from d2;
     t
------------
 2017-05-08
(1 row)

, thus to see the wanted format, use:

t=# set datestyle to DMY, SQL;
SET
t=# select * from d2;
     t
------------
 08/05/2017
(1 row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM