1

I have to convert one text column to integer column. Showed code work when I have text (all numbers) in fields but now I find some empty strings '' where query stops.

ALTER TABLE mytable 
ALTER COLUMN mycolumn TYPE integer USING (TRIM(mycolumn)::integer); 

Can here be done so that empty strings be converted to integer 0 so this query will pass?
How to do this?

2
  • 3
    concate your field with a '0'. something like (Trim('0'|mycolumns)::integer) Commented Dec 19, 2013 at 11:02
  • 1
    This works excellent, thank you very much! (Trim('0'||mycolumns)) Commented Dec 19, 2013 at 11:08

1 Answer 1

1

You can update your values before altering table:

UPDATE mytable SET mycolumn = '0' WHERE mycolumn = '';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.