I need to add column in table called value
which needs to store value from 0 to 1 up to 2 decimal points i.e. 0.25 0.50 0.75 0.80 etc..
Can anyone help with this? Thanks in advance
I need to add column in table called value
which needs to store value from 0 to 1 up to 2 decimal points i.e. 0.25 0.50 0.75 0.80 etc..
Can anyone help with this? Thanks in advance
2
Answers
Assuming that your specified range includes both bounds, use decimal(3,2) or numeric(3,2). Add a CHECK constraint to reject negative numbers. Online demo.
Test (note the rounding behaviour):
Example rejects:
I personally would use
smallint
and, store the value multiplied by 100 and add a check constraint that makes sure that the value is between 0 and 100.This minimizes storage space and makes calculations faster.
The down side is of course that some of your calculations have to be changed. Addition is pretty straightforward, but you’d have to divide by 100 after multiplying two numbers.