When you directly try to use
alter table alter Type integer you will get exception in postgres.
alter (column name of varying char type) TYPE integer USING charToInt(column name of varying char type );
So Using clause comes to rescue. It basically provides a mean (using a method)to convert the incoming data to appropriate type. So, you can try something as below:
CREATE OR REPLACE FUNCTION charToInt(charParam character varying)
RETURNS integer AS
$BODY$
SELECT CASE WHEN trim($1) SIMILAR TO '[0-9]+'
THEN CAST(trim($1) AS integer)
ELSE NULL END;
$BODY$
LANGUAGE 'sql' IMMUTABLE STRICT;
alter table (table name)
2 comments:
THANK YOU!!! works perfectly
Thank you men!!!
Post a Comment