Postgres:减少varchar大小并截断

Wil*_*nes 5 postgresql varchar truncate

我目前有一个包含varchar(10000)列的Postgres 8.4数据库.我想将其更改为varchar(255)并截断任何碰巧太长的数据.我怎样才能做到这一点?

mkj*_*mkj 12

就像是 ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(255) USING SUBSTR(c, 1, 255)


cod*_*eim 5

1) 使用子串方法更新列数据以截断它

update t set col = substring(col from 1 for 255)
Run Code Online (Sandbox Code Playgroud)

2)然后改变表列

alter table t alter column col type varchar(255)
Run Code Online (Sandbox Code Playgroud)

文档在这里http://www.postgresql.org/docs/8.4/static/sql-altertable.html

  • 似乎在最近的 pgsql 中应该是:update t set col = substring(col from 1 for 255)(“for”替换“to”) (2认同)