3

I want to convert column type from varchar to integer in postgresql

ALTER TABLE billdetail ALTER COLUMN  masterid TYPE integer;

gives

[Err] ERROR:  column "masterid" cannot be cast automatically to type integer
HINT:  You might need to specify "USING masterid::integer".

while

   ALTER TABLE billdetail USING masterid::integer;

gives

[Err] ERROR:  syntax error at or near "USING"
LINE 1: ALTER TABLE billdetail USING masterid::integer;

How can I fix this problem?

2 Answers 2

8

Try,

ALTER TABLE billdetail 
        ALTER COLUMN masterid TYPE INT USING masterid::integer;

You forgot to alter the column.

Sign up to request clarification or add additional context in comments.

Comments

0

ALTER TABLE table_name ALTER COLUMN col_name TYPE INT USING col_name::integer;

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

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.