在SQL中将int除以100时得到0而不是小数



我在myTable中有一列int类型的第1列,其值为,比方说,25。

cast(Column1 as decimal)/100插入临时表#tempTbl时,如下所示

create table #tempTbl
(
Column1 decimal
)
Insert into #tempTbl
select cast(Column1 as decimal)/100
from myTable

我在临时表的第1列中看到0,而不是0.25

当我尝试select cast(25 as decimal)/100时,我得到0.25

我做错了什么?

我建议:

select Column1 / 100.0

SQL Server执行整数除法(正如您所发现的(。只要在常数中包含一个小数位或乘以1.0通常就能解决这个问题。

尝试

select cast(Column1 as decimal)/100.0

如果执行

select 25/100.0;

它返回

(No column name)
0.250000

请查看此代码。在临时表的DDL中指定了小数长度

create table #tempTbl
(
Column1 decimal(8,2)
)
Insert into #tempTbl
select 25
select Column1/100.0 from #tempTbl;

输出

0.2500000

最新更新