1) create table
CREATE TABLE ad_position_ad (
p_id bigint ,
p_name character varying(20) ,
devid integer ,
ad_type character varying(20) ,
platform character varying(20) ,
category_id integer ,
config character varying(20) ,
is_check character varying(20) ,
status character varying(20) ,
showing_total bigint ,
click_total bigint ,
user_total bigint ,
income_total bigint ,
online_time integer ,
create_time integer ,
modify_time integer);
After create the table, we insert some data in the table.
2) Data of the table
mydb=> select count(*) from ad_position_ad;
count
-------
275
(1 row)
mydb=> select distinct config from ad_position_ad;
config
--------
2
0
1
(3 rows)
3) convert a varchar column to smallint
mydb=>
alter table ad_position_ad
alter column config type smallint using platform::smallint;
ALTER TABLE
4) check the data again
mydb=> select distinct config from ad_position_ad;
config
--------
3
(1 row)
Notice the value of column config have already changed. Does anybody know about that?
But the following command works fine, and the value does not change:
mydb=>
alter table ad_position_ad
alter column config type smallint using cast(config as smallint);
ALTER TABLE
Edit:
After rewriting the question I finally think I found the real problem. I quote:
You realize that you
ALTER COLUMNconfig, but use the value of the columnplatformfor it?Try: