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

前端 未结 5 1305
野性不改
野性不改 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条回答
  •  时光取名叫无心
    2020-12-13 12:28

    I think decimal(p, s) should be used while s represents the percentage capability. the 'p' could of been even 1 since we will never need more than one byte since each digit in left side of the point is one hunderd percent, so the p must be at least s+1, in order you should be able to store up to 1000%. but SQL doesn't allow the 'p' to be smaller than the s.

    Examples: 28.2656579879% should be decimal(13, 12) and should be stored 00.282656579879 128.2656579879% should be decimal(13, 12) and should be stored 01.282656579879

    28% should be stored in decimal(3,2) as 0.28 128% should be stored in decimal(3,2) as 1.28

    Note: if you know that you're not going to reach the 100% (i.e. your value will always be less than 100% than use decimal(s, s), if it will, use decimal(s+1, s).

    And so on

提交回复
热议问题