26

I tried to change precision like this:

ALTER Table account_invoice ALTER amount_total SET NUMERIC(5);

But I get syntax error, so I'm clearly doing something wrong. What is the right syntax to change precision of numeric in PostgreSQL?

4
  • 1
    alter table account_invoice alter column amount_type type decimal(5,0); ? or like this ALTER TABLE acc_inv MODIFY amount_total NUMERIC(5,0); ? Commented Feb 12, 2014 at 11:20
  • @VijaykumarHadalgi Post it as answer Commented Feb 12, 2014 at 11:22
  • i was taking a guess ! @ClodoaldoNeto i posted as answer ! Commented Feb 12, 2014 at 11:24
  • The right syntax is documented: postgresql.org/docs/current/static/sql-altertable.html Commented Feb 12, 2014 at 11:27

3 Answers 3

41

Try this:

ALTER Table account_invoice ALTER COLUMN amount_total TYPE DECIMAL(10,5);

DECIMAL(X, Y) -> X represents full length and Y represents precision of the number.

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

4 Comments

Just terms are a bit confusing in PostgreSQL here: postgresql.org/docs/9.1/static/datatype-numeric.html. As precision here is called full length of numbers. Also why DECIMAL not NUMERIC?
they are not different in postgresql you could use numeric as well. it maybe better indeed.
No need to change the data type to DECIMAL. This can also be done with data type NUMERIC as answered by Maike Mota.
Confirming Andrius' comment, I believe the X,Y in this answer are switched. X is precision and Y is Scale - postgresql.org/docs/14/datatype-numeric.html#id-1.5.7.9.7.4 (link to exact paragraph)
16

You can use this:

ALTER Table account_invoice ALTER amount_total SET DATA TYPE NUMERIC(5,Y);

where Y is your required level of precision.

3 Comments

FYI: 5 is precision and Y is scale
I can see the confusion thats my comment about the Y's value can cause, when I said "precision" I wanted to mean the amount of decimal places, the POSTGRE documentation refers to precision as the total number of digits that a number could contains and the scale as the total of digits reserved to be fractional (AKA. decimal places). Thanks for bring my attention to this!
NUMERIC(precision, scale). The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero. source
7

You need to ue the TYPE keyword after the column name, not SET

ALTER Table account_invoice ALTER amount_total TYPE NUMERIC(5);

See the docs: ALTER TABLE

Example on SQL Fiddle

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.