stef
stef

Reputation: 27749

MySQL field type for storing decimals

I'm creating a DB that will hold products with several "height" columns (in meters, for ex 7.79 or 12,8). Never more than 2 digits before and 2 after the decimal point. What field type should I use for this?

If I use decimal(2,2) an try to insert 7.79 in phpmyadmin I get an error saying Warning: #1264 Out of range value for column 'working_height' at row 1

I'll be using this DB for searching, so I have to be able to run a query like "select all products where height is great than 7".

Upvotes: 4

Views: 14788

Answers (3)

user159088
user159088

Reputation:

People will say to use decimal(s, d) but how about storing the values as integers, in centimeters instead of meters? Easier to compare (no precision loss).

Just my two cents.

Upvotes: 7

dsolimano
dsolimano

Reputation: 8986

You're looking for decimal(4,2) - in general, decimal(m,n) means m total digits, and n to the right of the decimal point. Docs here.

So a decimal(2,2) can store two total digits, both to the right of the decimal point. This explains the error that you are seeing.

Upvotes: 10

quantumSoup
quantumSoup

Reputation: 28132

Try DECIMAL(4,2) instead

Refer to: MySQL Numeric Types

Upvotes: 4

Related Questions