SQL Server 中数字、浮点数和十进制数的区别 [英] Difference between numeric, float and decimal in SQL Server

查看:24
本文介绍了SQL Server 中数字、浮点数和十进制数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numericfloatdecimal 数据类型有什么区别,应该在什么情况下使用?

对于任何类型的金融交易(例如工资领域),哪一种是首选,为什么?

解决方案

使用floatreal 数据类型仅当decimal 提供的精度(最多 38 位)) 不足

  • 近似数字数据类型不会存储为许多数字指定的确切值;它们存储非常接近的近似值.( - 第 3 章 - 表、数据类型和声明性数据完整性第 1 课 - 选择数据类型(指南) - 第 93 页 >

    What are the differences between numeric, float and decimal datatypes and which should be used in which situations?

    For any kind of financial transaction (e.g. for salary field), which one is preferred and why?

    解决方案

    use the float or real data types only if the precision provided by decimal (up to 38 digits) is insufficient

    • Approximate numeric data types do not store the exact values specified for many numbers; they store an extremely close approximation of the value.(Technet)

    • Avoid using float or real columns in WHERE clause search conditions, especially the = and <> operators (Technet)

    so generally because the precision provided by decimal is [10E38 ~ 38 digits] if your number can fit in it, and smaller storage space (and maybe speed) of Float is not important and dealing with abnormal behaviors and issues of approximate numeric types are not acceptable, use Decimal generally.

    more useful information

    • numeric = decimal (5 to 17 bytes) (Exact Numeric Data Type)
      • will map to Decimal in .NET
      • both have (18, 0) as default (precision,scale) parameters in SQL server
      • scale = maximum number of decimal digits that can be stored to the right of the decimal point.
      • kindly note that money(8 byte) and smallmoney(4 byte) are also exact and map to Decimal In .NET and have 4 decimal points(MSDN)
      • decimal and numeric (Transact-SQL) - MSDN
    • real (4 byte) (Approximate Numeric Data Type)
    • float (8 byte) (Approximate Numeric Data Type)
      • will map to Double in .NET
    • All exact numeric types always produce the same result, regardless of which kind of processor architecture is being used or the magnitude of the numbers
    • The parameter supplied to the float data type defines the number of bits that are used to store the mantissa of the floating point number.
    • Approximate Numeric Data Type usually uses less storage and have better speed (up to 20x) and you should also consider when they got converted in .NET

    main source : MCTS Self-Paced Training Kit (Exam 70-433): Microsoft® SQL Server® 2008 Database Development - Chapter 3 - Tables , Data Types , and Declarative Data Integrity Lesson 1 - Choosing Data Types (Guidelines) - Page 93

    这篇关于SQL Server 中数字、浮点数和十进制数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆