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

问题描述:

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

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

decimal(p, s) 和 numeric(p, s)

decimal(p, s) and numeric(p, s)

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(比例):

将存储在小数点右侧的小数位数(-> s 定义小数位数)

The number of decimal digits that will be stored to the right of the decimal point (-> s defines the number of decimal places)

0

0 <= s <= p.

  • p ... 总位数
  • s ... 小数点右边的位数
  • p-s ... 小数点左边的位数

示例:

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;

结果:

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

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

链接:msdn.microsoft.com