简体   繁体   English

SQL 从整数转换为十进制

[英]SQL Cast from int to decimal

I have two columns in a table that are populated with integer values.我在表中有两列,其中填充了 integer 个值。 For each record, I want to divide the values of those two columns and format the output so that the decimal point is moved to places to the right.对于每条记录,我想划分这两列的值并格式化 output,以便将小数点移到右边的位置。

For example, if I have values of the two columns for one record as 4 and 1 and I want to divide 1 by 4 (so 1/4) then I want the output to be 25.00.例如,如果一条记录的两列的值为 4 和 1,并且我想将 1 除以 4(即 1/4),那么我希望 output 为 25.00。

Here is the last thing I tried a bit ago:这是我之前尝试的最后一件事:

CAST(Total AS Decimal(2))/CAST(TotalAnswers AS Decimal(2)) AS 'Percent' 

I have not been able to find a syntactical explanation of the CAST function to see what the parameter passed in for decimal represents.我一直没能找到 CAST function 的句法解释来查看传递给 decimal 的参数代表什么。 When I change it, it does sometimes change the number of places to the right of the decimal but the results are not always what I expect so I would like to get a little help on this.当我改变它时,它有时会改变小数点右边的位数,但结果并不总是我所期望的,所以我想在这方面得到一些帮助。

Write it like this:像这样写:

SELECT CAST(CAST(Total As float) / TotalAnswers * 100 As decimal(8, 2))

DECIMAL(2) is a decimal of 2 digits before and no digits after the decimal point. DECIMAL(2)是小数点前 2 位小数,小数点后无数字。 That doesn't allow you to have digits after the decimal point!那不允许你在小数点有数字!

The DECIMAL(p,s) specification defines the number of total digits ( p ) and the number of digits after the decimal point ( s ) (where s cannot be larger than p and is 0 if ommitted). DECIMAL(p,s)规范定义了总位数 ( p ) 和小数点后的位数 ( s )(其中s不能大于p ,如果省略则为 0)。

You need to use something like DECIMAL(4,2) ( 4 digit total - 2 of which after the decimal point; so therefore: also 2 before the decimal point ) or something like that - some digits before and some after the decimal point - then you'll see your desired result!您需要使用DECIMAL(4,2)之类的东西(总共 4 位数字 - 其中小数点后有 2 位;因此:小数点前还有 2 位)或类似的东西 - 小数点前的一些数字和小数点后的一些数字 -然后你会看到你想要的结果!

For details on the DECIMAL type and its syntax, consult the MSDN Books Online topic on decimal and numeric有关DECIMAL类型及其语法的详细信息,请参阅 MSDN 联机丛书中关于decimal 和 numeric的主题

There are actual a couple of parameters..实际上有几个参数..

(p, s)

where在哪里

  • p represents the "precision" - the total number of digits before and after the decimal point. p代表“精度”——小数点前后的总位数。
  • s represents the "scale" - the number of digits after the decimal point. s 代表“刻度”——小数点的位数。

By only giving 1 parameter, you are giving the precision only.通过仅提供 1 个参数,您仅提供精度。

See MDSN .请参阅MDSN

from book-online for decimal data type: Numeric data types that have fixed precision and scale.来自联机书籍的十进制数据类型:具有固定精度和小数位数的数字数据类型。

decimal[ (p[, s] )] and numeric[ (p[, s] )] Fixed precision and scale numbers. decimal[ (p[, s] )] 和 numeric[ (p[, s] )] 固定精度和小数位数。 When maximum precision is used, valid values are from - 10^38 +1 through 10^38 - 1. The SQL-92 synonyms for decimal are dec and dec(p, s).使用最大精度时,有效值为 - 10^38 +1 到 10^38 - 1。小数的 SQL-92 同义词是 dec 和 dec(p, s)。 numeric is functionally equivalent to decimal. numeric 在功能上等同于 decimal。

p (precision) The maximum total number of decimal digits that can be stored, both to the left and to the right of the decimal point. p(精度)可以存储的小数点左侧和右侧的最大小数位数。 The precision must be a value from 1 through the maximum precision of 38. The default precision is 18.精度必须是 1 到最大精度 38 之间的值。默认精度为 18。

s (scale) The maximum number of decimal digits that can be stored to the right of the decimal point. s (scale) 小数点右边可以存储的最大小数位数。 Scale must be a value from 0 through p.比例必须是从 0 到 p 的值。 Scale can be specified only if precision is specified.仅当指定精度时才能指定比例。 The default scale is 0;默认比例为 0; therefore, 0 <= s <= p.因此,0 <= s <= p。 Maximum storage sizes vary, based on the precision.最大存储大小因精度而异。

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

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