简体   繁体   English

如何在SQL中将int转换为十进制

[英]how to convert int to decimal in SQL

I am working on an to solve a problem that I have a textbox that accepts any type of data but I want to cast the data to decimal(12,9) or any data type can accept this number as an example 42.56926437219384 我正在解决一个问题,我有一个文本框可以接受任何类型的数据,但我想将数据转换为decimal(12,9)或任何数据类型都可以接受此数字作为示例42.56926437219384

here is the problem the user can enter any kind of data like characters or integer 这是用户可以输入任何类型的数据(例如字符或整数)的问题

1st case I want to handle if the data entered as integer: 第一种情况,我想处理是否将数据输入为整数:

DECLARE @num DECIMAL(12,9) = 444444444444444
SELECT CONVERT(DECIMAL(12,9),@num)

I think if it is characters will handle it in the solution and assign validation for the textbox. 我认为如果是字符,它将在解决方案中处理它并为文本框分配验证。

how can I handle the integer part? 如何处理整数部分?

When you specify it as DECIMAL(12,9) this means your number can have up to 12 digits (excluding . ) and out of that 9 digits will be after decimal part. 当您将其指定为DECIMAL(12,9)时,这意味着您的数字最多可以包含12位数字(不包括。),而这9位数字中的小数点后。 Which means the maximum value that you can store here is 999.999999999 . 这意味着您可以在此处存储的最大值是999.999999999

in your sample scenario above, the number has got 15 integer parts which are too high than the scope of your variable, as you can do either of the following 在上面的示例场景中,该数字有15个整数部分,它们超出变量的范围,您可以执行以下任一操作

  1. Change the size of variable from DECIMAL(12,9) to higher integer precision like DECIMAL(25,9) 将变量的大小从DECIMAL(12,9)更改为更高的整数精度,例如DECIMAL(25,9)
  2. Add a validation on your application to restrict the number of characters that the user can enter 在您的应用程序上添加验证以限制用户可以输入的字符数

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

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