32

How can I store negative value in mysql decimal? I have data from DMS to Decimal having negative values, so it is decimal and negative. So what can I use to store such value?

Hafiz
  • 4,187
  • 12
  • 58
  • 111
  • Column of type `numeric` or any flavour of myriad of floats available with MySQL and then simply use `INSERT INTO your_table SET your_number = '-1000.99';`, replace the SQL command with one you need of course. – N.B. Dec 27 '11 at 11:38
  • You can store either positive or negative values in Decimal columns. WHat have you tried? – ypercubeᵀᴹ Dec 27 '11 at 11:38
  • possible duplicate of : http://stackoverflow.com/questions/3760822/storing-negative-number-in-decimal-field-of-mysql-table-as-of-version-5-0-3 – xQbert Dec 27 '11 at 11:39
  • @xQbert: I'd say that doesn't apply. The other Q is about changes related over different versions, whereas this is "what type do I use" – gbn Dec 27 '11 at 11:42
  • 1
    Possibly just some confusion over signed vs unsigned? – dash Dec 27 '11 at 11:59
  • I wrote some thing from mysql site that it doesnot deal with negative number but maybe I didn't read correctly as I was going through many things at that time. So let me try negative – Hafiz Dec 27 '11 at 16:01

2 Answers2

63

Use MySQL DECIMAL type?

Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99

gbn
  • 422,506
  • 82
  • 585
  • 676
3

In case you want to store a negative integer - (INT) will work nicely, unless it's not UNSIGNED.

https://dev.mysql.com/doc/refman/8.0/en/integer-types.html

In case you want to store a negative decimal - (DECIMAL) is the way to go, as GBN mentioned above.

https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html

Rossitten
  • 1,140
  • 1
  • 16
  • 34