What should be the best way to store a percent value in SQL-Server?

前端 未结 5 1315
野性不改
野性不改 2020-12-13 12:12

I want to store a value that represents a percent in SQL server, what data type should be the prefered one?

5条回答
  •  Happy的楠姐
    2020-12-13 12:39

    You should use decimal(p,s) in 99.9% of cases.

    Percent is only a presentation concept: 10% is still 0.1.

    Simply choose precision and scale for the highest expected values/desired decimal places when expressed as real numbers. You can have p = s for values < 100% and simply decide based on decimal places.

    However, if you do need to store 100% or 1, then you'll need p = s+1.

    This then allows up to 9.xxxxxx or 9xx.xxxx%, so I'd add a check constraint to keep it maximum of 1 if this is all I need.

提交回复
热议问题