在TSQL中,Float,real和Double是浮点型,存在精度缺失,是近似数值,近似数字数据类型并不存储为多数数字指定的精确值,它们只储存这些值的最近似值。Decimal和Numeric是精确数值,不存在精度损失。当数据值必须严格按指定存储时,可以使用 decimal 或numeric数据类型来存储带小数的数字。
第一类,近似数值,存在精度损失
Syntax
The SQL Server float[(n)] data type complies with the ISO standard for all values of n from 1 through 53.
The synonym for double precision is float(53).
The ISO synonym for real is float(24).
Note: double precision 组合作为一个DataType
In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.
示例1,由于在TSQL中,小数点会自动转换为numeric类型,1.0自动转换为numeric(2, 1)
--float(24) declare @f_low float(24) declare @r real select @f_low=cast(1.0 as float(53))/3,@r=cast(1.0 as float(53))/3 select 1.0/3 as f,@f_low as f_low,@r as r --float(53) declare @f float declare @dp double PRECISION declare @f_high float(53) select @f=cast(1.0 as float(53))/3,@dp=cast(1.0 as float(53))/3,@f_high=cast(1.0 as float(53))/3 select @f as f,@dp as d,@f_high as f_high
在 WHERE 子句搜索条件(特别是 = 和 <> 运算符)中,应避免使用 float 列或 real 列。float 列和 real 列最好只限于 > 比较或 < 比较。
第二类,精确数值,不存在精度损失
Numeric data types that have fixed precision and scale.
Note:p 和 s 必须遵守规则:0 <= s <= p <= 38
numeric 和 decimal 数据类型的默认最大精度为 38。在 Transact-SQL 中,numeric 的功能等同于 decimal 数据类型。
decimal 数据类型最多可以存储 38 个数字,所有这些数字均可位于小数点后面。decimal 数据类型存储精确的数字表示形式,存储值没有近似值。
定义 decimal 列、变量和参数的两种属性为:
p 指定精度或对象能够支持的数字个数。
s 指定可以放在小数点右边的小数位数或数字个数。
numeric 和 decimal 数据类型的默认最大精度为 38。在 Transact-SQL 中,numeric 的功能等同于 decimal 数据类型。
当数据值必须严格按指定存储时,可以使用 decimal 数据类型来存储带小数的数字。
示例2,
DECLARE @dec decimal(38,37) declare @num numeric(38,37) select @dec=cast(1.0 as decimal(38,37))/3,@num=cast(1.0 as NUMERIC(38,37))/3 select @dec,@num
Note:
In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.
By default, SQL Server uses rounding when converting a number to a decimal or numeric value with a lower precision and scale. However, if the SET ARITHABORT option is ON, SQL Server raises an error when overflow occurs. Loss of only precision and scale is not sufficient to raise an error.
在 Transact-SQL 语句中,带有小数点的常量自动转换为 numeric 数据值,且必然使用最小的精度和小数位数。例如,常量 12.345 被转换为 numeric 值,其精度为 5,小数位为 3。
默认情况下,在将数字转换为较低精度和小数位数的 decimal 或 numeric 值时,SQL Server 使用舍入法。然而,如果 SET ARITHABORT 选项为 ON,当发生溢出时,SQL Server 会出现错误。若仅损失精度和小数位数,则不会产生错误。
float,double precision,real, decimal,numeric 用法探究
原文:http://www.cnblogs.com/ljhdo/p/4910699.html