简体   繁体   English

在SQL-Server中存储百分比值的最佳方法是什么?

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

我想存储一个表示SQL Server中百分比的值,应该选择哪种数据类型?

You should use decimal(p,s) in 99.9% of cases. 您应该在99.9%的情况下使用小数(p,s)

Percent is only a presentation concept: 10% is still 0.1. 百分比只是一个表达概念:10%仍然是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. 对于值<100%,您可以使用p = s,并根据小数位数进行简单决定。

However, if you do need to store 100% or 1, then you'll need p = s+1. 但是,如果您确实需要存储100%或1,那么您将需要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. 这允许最多9.xxxxxx或9xx.xxxx%,所以我添加一个检查约束,以保持最大值1,如果这是我需要的全部。

decimal(p, s) and numeric(p, s) 十进制(p,s)和数字(p,s)

p (precision): p(精度):

The maximum total number of decimal digits that will be stored (both to the left and to the right of the decimal point) 将存储的最大小数位数(小数点的左侧和右侧)


s (scale): s(规模):

The number of decimal digits that will be stored to the right of the decimal point (-> s defines the number of decimal places) 将存储在小数点右侧的小数位数( - > s定义小数位数)


0 <= s <= p. 0 <= s <= p。

  • p ... total number of digits p ...总位数
  • s ... number of digits to the right of the decimal point s ...小数点右边的位数
  • ps ... number of digits to the left of the decimal point ps ...小数点左边的位数

Example: 例:

CREATE TABLE dbo.MyTable
( MyDecimalColumn decimal(5,2)
 ,MyNumericColumn numeric(10,5)
);

INSERT INTO dbo.MyTable VALUES (123, 12345.12);

SELECT MyDecimalColumn, MyNumericColumn FROM dbo.MyTable;

Result: 结果:

MyDecimalColumn: 123.00 (p=5, s=2)

MyNumericColumn: 12345.12000 (p=10, s=5)

link: msdn.microsoft.com 链接:msdn.microsoft.com

I agree, DECIMAL is where you should store this type of number. 我同意,DECIMAL是您应该存储此类号码的地方。 But to make the decision easier, store it as a percentage of 1, not as a percentage of 100. That way you can store exactly the number of decimal places you need regardless of the "whole" number. 但是为了使决策更容易,将其存储为1的百分比,而不是100的百分比。这样,无论“整数”如何,您都可以精确存储所需的小数位数。 So if you want 6 decimal places, use DECIMAL(9, 8) and for 23.3436435%, you store 0.23346435. 因此,如果您想要6个小数位,请使用DECIMAL(9,8)和23.3436435%,存储0.23346435。 Changing it to 23.346435% is a display problem, not a storage problem, and most presentation languages / report writers etc. are capable of changing the display for you. 将其更改为23.346435%是显示问题,而不是存储问题,并且大多数演示语言/报表编写者等都能够为您更改显示。

I think decimal(p, s) should be used while s represents the percentage capability. 我认为应该使用decimal(p,s),而s代表百分比能力。 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%. 'p'可以是1,因为我们永远不需要超过一个字节,因为该点左侧的每个数字都是一个百分比,所以p必须至少为s + 1,以便你能够存储高达1000%。 but SQL doesn't allow the 'p' to be smaller than the s. 但SQL不允许'p'小于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.2656579879%应为十进制(13,12)并应存储00.282656579879 128.2656579879%应为十进制(13,12)并应存储01.282656579879

28% should be stored in decimal(3,2) as 0.28 128% should be stored in decimal(3,2) as 1.28 28%应存储在十进制(3,2)中,因为0.28 128%应以十进制(3,2)存储为1.28

Note: if you know that you're not going to reach the 100% (ie your value will always be less than 100% than use decimal(s, s), if it will, use decimal(s+1, s). 注意:如果你知道你不会达到100%(即你的值总是小于100%而不是使用小数(s,s),如果是,则使用小数(s + 1,s)。

And so on 等等

列的数据类型应为十进制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM