简体   繁体   English

Postgres表的列的ALTER DATATYPE

[英]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. 我刚开始使用Postgres数据库并创建了几个表。为方便起见,最初我使用了character changes(50),但现在我想将其更改为Date(dd / mm / yyyy)格式。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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