sql二进制转十进制

This article aims to walk you through the SQL Decimal data type and its usage with various examples. We will also see how we can exercise this data type in SQL Server to help make SQL developer’s job easier.

本文旨在通过各种示例逐步介绍SQL Decimal数据类型及其用法。 我们还将看到如何在SQL Server中使用此数据类型,以帮助简化SQL开发人员的工作。

介绍 (Introduction)

Organizations deal with decimals on a day-to-day basis, and these decimal values can be seen everywhere in different sectors, be it in banks, the medical industry, biometrics, gas stations, financial reports, sports, and whatnot. Using whole numbers (by rounding decimal numbers) definitely makes one’s job easier but it often leads to inaccurate outputs, especially when we are dealing with a large number of values and crucial data. In such scenarios, it is ideal to use Sql Decimal data type in SQL Server to deliver correct results with perfect precision.

组织每天处理小数,这些十进制值可以在银行,医疗行业,生物识别,加油站,财务报告,体育等各种地方的不同部门中随处可见。 使用整数(通过四舍五入十进制数)无疑会使人的工作更轻松,但通常会导致输出不准确,尤其是当我们处理大量的值和关键数据时。 在这种情况下,理想的是在SQL Server中使用Sql Decimal数据类型来以完美的精度提供正确的结果。

It becomes very essential for SQL developers to choose the correct data types in the table structure while designing and modeling SQL databases. Let’s move forward and explore Decimal data type in SQL Server.

对于SQL开发人员来说,在设计和建模SQL数据库时选择表结构中的正确数据类型变得非常重要。 让我们继续前进,探索SQL Server中的Decimal数据类型。

前提条件 (Pre-requisite )

SQL Decimal data type is being used in SQL Server since forever. You can use any SQL Server version installed (starting 2000 or above) to understand this data type. We will be using SQL Server 2017 in this article for the demo purposes. If you don’t have any version installed on your system and wish to practice against the 2017 version, download it from here.

SQL Decimal数据类型从一开始就在SQL Server中使用。 您可以使用安装的任何SQL Server版本(从2000或更高版本开始)来了解此数据类型。 我们将在本文中使用SQL Server 2017进行演示。 如果您的系统上未安装任何版本,并希望针对2017版本进行练习,请从此处下载。

SQL Server中的十进制数据类型的基本语法 (The Basic syntax of Decimal data type in SQL Server)

Let’s take a look at the basic syntax of SQL Decimal Data type first. It is denoted as below:

首先让我们看一下SQL Decimal Data类型的基本语法。 表示如下:

  • decimal [(p [,s])] 十进制[[p [,s])]

Where,

哪里,

  • p stands for Precision, the total number of digits in the value, i.e. on both sides of the decimal point p代表精度,即值中的位数总数,即小数点的两侧
  • s stands for Scale, number of digits after the decimal point s代表Scale,小数点后的位数

The default value of p is 18 and s is 0 and for both these values, the minimum is 1 and the maximum is 38.

p的默认值为18,s的默认值为0,这两个值的最小值均为1,最大值为38。

In short, by defining parameters in the SQL Decimal data type, we are estimating how many digits a column or a variable will have and also the number of digits to the right of the decimal point.

简而言之,通过在SQL Decimal数据类型中定义参数,我们可以估算列或变量将具有多少位数,以及小数点右边的位数。

For instance, decimal (4,2) indicates that the number will have 2 digits before the decimal point and 2 digits after the decimal point, something like this has to be the number value- ##.##.

例如,十进制(4,2)表示该数字在小数点前有2位数字,在小数点后有2位数字,类似这样的东西必须是数字值-##。##。

One important thing to note here is, – parameter s (Scale) can only be specified if p (Precision) is specified. The scale must always be less than or equal to the precision.

这里要注意的重要一件事是:–仅当指定了p(精度)时才能指定参数s(比例)。 比例尺必须始终小于或等于精度。

定义SQL十进制数据类型 (Defining SQL Decimal Data type)

Let’s work with a very popular mathematical constant – π, aka, Pi that has a value equal to 3.14159 (22/7 in a fraction). Copy and paste the below query in a new query window and execute it.

让我们使用一个非常流行的数学常数–π,又称Pi,其值等于3.14159(分数为22/7)。 将以下查询复制并粘贴到新的查询窗口中并执行。

DECLARE @PiWithNoDecimal DECIMAL(6,0) = 3.14159
DECLARE @Piupto5Decimal DECIMAL(6,5) = 3.14159
DECLARE @Piupto1Decimal DECIMAL(3,1) = 3.14159
SELECT @PiWithNoDecimal AS PiWithNoDecimal, @Piupto5Decimal AS Piupto5Decimal, @Piupto1Decimal AS Piupto1Decimal

The above result set shows how SQL Server treats each combination of precision and scale as a different data type. Like here, decimal (6, 0) behaves differently from data types decimal (6,5) and decimal (3,1) and are considered as three different types. This way we can tweak the parameters in the SQL Decimal type to achieve desired results.

上面的结果集显示了SQL Server如何将精度和小数位的每种组合视为不同的数据类型。 像这里一样,十进制(6,0)的行为不同于数据类型十进制(6,5)和十进制(3,1),并被认为是三种不同的类型。 这样,我们可以调整SQL Decimal类型中的参数以获得所需的结果。

Now that we know how to create this Decimal data type in SQL Server, let’s explore it with numerous examples.

现在,我们知道如何在SQL Server中创建此Decimal数据类型,让我们通过大量示例进行探索。

在表中使用SQL十进制 (Using SQL Decimal in the Tables)

Let’s quickly create a new table, named Patients, that makes use of decimal data type for columns height and weight. We will insert a few rows using an INSERT clause as shown below for the demo purposes.

让我们快速创建一个名为Patients的新表,该表将十进制数据类型用于列的高度和重量。 为了演示的目的,我们将使用INSERT子句插入几行,如下所示。

CREATE TABLE dbo.Patients
( Name varchar(10),Gender varchar(2),Height decimal (3,2),Weight decimal (5,2)
)
INSERT INTO PATIENTS VALUES('John','M',6.1,80.4)
INSERT INTO PATIENTS VALUES('Bred','M',5.8,73.7)
INSERT INTO PATIENTS VALUES('Leslie','F',5.3,66.9)
INSERT INTO PATIENTS VALUES('Rebecca','F',5.7,50.2)
INSERT INTO PATIENTS VALUES('Shermas','M',6.5,190.6)

Once the data is populated in the table, we can query this data using SELECT statement as shown below. The decimal values can be seen in the height and weight attributes.

将数据填充到表中后,我们可以使用SELECT语句查询此数据,如下所示。 可以在身高和体重属性中看到十进制值。

SELECT * FROM dbo.PATIENTS

Let’s figure out what happens if we try to insert values that exceed the specified precision or scale values while defining the Height and Weight columns. For this demo, we will insert 2 more rows into this table (shown below).

让我们弄清楚如果在定义“高度”和“重量”列时尝试插入超出指定精度或比例值的值会发生什么。 对于此演示,我们将在此表中再插入2行(如下所示)。

  1. INSERT INTO PATIENTS VALUES('Largest','M', '10.9', 88.5)
    
  2. INSERT INTO PATIENTS VALUES('Hulk','M', '9.9', 1000.45)
    

It encounters the below error saying arithmetic overflow error and the SQL Server terminated the statements.

它遇到以下错误,指出算术溢出错误,并且SQL Server终止了该语句。

Let’s get to the root of this issue:

让我们弄清楚这个问题的根源:

  • Height Decimal (3, 2) means the value can have 3 digits overall and 2 digits to the right of the decimal point. In the first line of code above, the value 10.9 (considered as 10.90 = 4 digits overall) exceeds the specified range (3, 2) and causes the overflow 高度小数 (3,2)表示该值的总位数可以为3位,小数点右边可以为2位。 在上面的代码的第一行中,值10.9(视为10.90 =总共4位数字)超出指定范围(3,2),并导致溢出
  • Weight Decimal (5,2) means the total number of digits cannot exceed 5 and 2 digits can be placed to the right of the decimal. However, the value 1000.45 in the second line of code above exceeds the specified range of (5, 2) since it means 6 digits in total and throws an overflow error 十进制权重 (5,2)表示总位数不能超过5,并且2位数字可以放在小数点右边。 但是,上面第二行代码中的值1000.45超出了指定的范围(5,2),因为它表示总共6位数并且会引发溢出错误
  • Quick note – In the above error message, if you have noticed, “data type numeric” is stated instead of data type decimal, the reason is that the Decimal and the Numeric data type are exactly the same, both are fixed-precision data types and can be used interchangeably 快速说明 –在上述错误消息中,如果您已经注意到,声明的是“数据类型数字”而不是数据类型十进制,原因是十进制和数字数据类型完全相同,均为固定精度数据类型可以互换使用

解决错误 (Resolving the error)

One of the easiest workarounds is to increase the precision level of the columns to store bigger numbers. We can alter the data type of the columns without dropping the table or column with the below code.

最简单的解决方法之一是增加列的精度级别以存储更大的数字。 我们可以更改列的数据类型,而无需使用以下代码删除表或列。

ALTER TABLE dbo.Patients ALTER COLUMN Height decimal(4,2)
ALTER TABLE dbo.Patients ALTER COLUMN Weight decimal (6,2)

Once altered, execute the Insert queries to insert these rows into the table.

更改后,执行插入查询以将这些行插入表中。

We can see the rows being added to the table.

我们可以看到将行添加到表中。

SQL Server中使用十进制数据类型的存储注意事项 (Storage considerations with Decimal Data Type in SQL Server)

Data type SQL Decimal requires the following storage bytes for the specified precision as provided by Microsoft below:

数据类型SQL Decimal需要以下存储字节以达到指定精度,如Microsoft所提供的:

Precision

Storage (Bytes)

1 – 9

5

10 – 19

9

20 – 28

13

29 – 38

17

精确

储存空间(位元组)

1 – 9

5

10 – 19

9

20 – 28

13

29 – 38

17

The space consumption of SQL Decimal data type is based on the column definition and not on the size of the value being assigned to it. For e.g. Decimal (12, 4) with value of 888.888 takes 9 bytes on disk and Decimal (22, 2) value of 9999.99 consumes 13 bytes on disk. This is why this data type falls under fixed-length columns.

SQL Decimal数据类型的空间消耗取决于列定义,而不取决于为其分配的值的大小。 例如,值888.888的十进制(12,4)在磁盘上占用9个字节,而值9999.99的十进制(22,2)在磁盘上消耗13个字节。 这就是为什么此数据类型属于固定长度列的原因。

As a SQL developer myself, I always try to use SQL Decimal data type as decimal (9, 2) which consumes the least storage, 5 bytes on disk and offers better performance.

作为我自己SQL开发人员,我总是尝试将SQL Decimal数据类型用作十进制(9,2),它消耗最少的存储空间,磁盘上的5个字节并提供更好的性能。

结论 (Conclusion)

I hope this article provides a comprehensible approach on how to use SQL Decimal data type. Always ensure the precision of the decimal or numeric variable specified is enough to accommodate the values assigned to it. Additionally, we observed, how selecting the right kind of data type helps SQL developers to save disk storage.

我希望本文提供了一种有关如何使用SQL Decimal数据类型的可理解方法。 始终确保指定的十进制或数字变量的精度足以容纳分配给它的值。 此外,我们观察到,选择正确的数据类型如何帮助SQL开发人员节省磁盘存储。

In case of any questions, please feel free to ask in the comments section below.

如有任何疑问,请随时在下面的评论部分中提问。

To continue your journey with SQL Server and data types used in it, I would recommend going through the below links.

为了继续使用SQL Server及其中使用的数据类型,我建议您浏览以下链接。

  • Spatial SQL data types in SQL Server SQL Server中的空间SQL数据类型
  • SQL Server Data Type Conversion Methods and performance comparison SQL Server数据类型转换方法和性能比较
  • Understanding the GUID data type in SQL Server 了解SQL Server中的GUID数据类型
  • A step-by-step walkthrough of SQL Inner Join SQL内部联接的分步演练

翻译自: https://www.sqlshack.com/understanding-sql-decimal-data-type/

sql二进制转十进制

sql二进制转十进制_了解SQL十进制数据类型相关推荐

  1. sql子查询示例_学习SQL:SQL查询示例

    sql子查询示例 In the previous article we've practiced SQL, and today, we'll continue with a few more SQL ...

  2. 学习sql注入:猜测数据库_学习SQL:SQL数据类型

    学习sql注入:猜测数据库 What are SQL data types, why do we need them, and how to use them? Today, we'll try to ...

  3. sql server java类型_使用基本 JDBC 数据类型 - SQL Server | Microsoft Docs

    使用基本数据类型Using basic data types 01/29/2021 本文内容 Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Dr ...

  4. sql 触发器未触发_学习SQL:SQL触发器

    sql 触发器未触发 SQL Triggers are another powerful database object we have at our disposal. In previous ar ...

  5. sql活动监视器 死锁_监视SQL Server死锁–简单方法

    sql活动监视器 死锁 SQL Server is a very powerful tool and wherever I go, I see the tool being way much unde ...

  6. 复杂sql 查询编写方法_学习SQL:如何编写复杂的SELECT查询

    复杂sql 查询编写方法 In my career, I've heard many times, things like "How to write a complex SELECT qu ...

  7. 学习sql注入:猜测数据库_学习SQL:删除和更新数据SQL最佳实践

    学习sql注入:猜测数据库 Deleting and updating data is very common, but if performed without taking care, which ...

  8. sql 闩锁 原因_关于SQL Server中的闩锁

    sql 闩锁 原因 SQL Server locks, discussed in the article All about locking in SQL Server, which is appli ...

  9. mysql sql注入很常用_常见sql注入的类型

    这里只讲解sql注入漏洞的基本类型,代码分析将放在另外一篇帖子讲解 目录 最基础的注入-union注入攻击 Boolean注入攻击-布尔盲注 报错注入攻击 时间注入攻击-时间盲注 堆叠查询注入攻击 二 ...

最新文章

  1. 5G NGC — UDR 统一数据存储库,UDSF 非结构化数据存储功能
  2. 视音频数据处理入门:AAC音频码流解析
  3. leetcode 149. 直线上最多的点数
  4. vb检测html事件,VB代码VB小程序:捕获 WebBrowser 控件的鼠标事件
  5. android 软件 加密方法,Android中WIFI常见的几种加密方式(详细)
  6. 8086 MOV 指令的注意事项
  7. 企业微信api接口,企业微信sdk
  8. UA MATH524 复变函数 用保形映射解Laplace方程的边值问题
  9. 怎么让电脑微信安装到别的盘路径
  10. Northwind数据库下载地址
  11. win10系统64位( 惠普)台式电脑自动开机、关机图文详细解(一)
  12. java list筛选数据_java 根据条件在List中筛选出符合条件的对象
  13. 详解Linux运维工程师必备技能
  14. 腾讯云短信服务错误码列表
  15. 点下确认的那一刻,我的大脑在发生些什么?
  16. dw模板文件的扩展名_模板文件的扩展名是() - 问答库
  17. [云数据中心] 《云数据中心网络架构与技术》读书笔记 第七章 构建多数据中心网络(2/3)
  18. 058输入月份号输出英文月份名
  19. Facebook发的不是币, 而是世界变革的信号弹
  20. 谷歌自行车开始销售,看完后汽车都不想开了!

热门文章

  1. python随机生成验证码_Python生成随机验证码的两种方法
  2. gtest测试框架使用详解_python selenium自动化测试框架如何搭建使用?
  3. C语言系列之自增自减运算符的用法(二)
  4. javascript中使用 闭包重载函数,记录日志功能
  5. iOS开发 网络编程 Socket编程
  6. 客户端控件Javascript验证类
  7. 【博客项目】—案例初始化(二)
  8. RFID 是什么意思
  9. 为什么坚持一件事总是那么难,而且有时候总是三分钟热度?
  10. 退休的姐妹们,你们还打工吗?