Ala*_*laa 3 sql t-sql sql-server
我有一个十进制类型的列,我需要在它上面使用sum函数,如下所示:
declare @credit decimal = (select
( select ISNULL(SUM(Convert(DECIMAL(13,2), Amount)),0)
from TransactionDetail as t1
where t1.AccountFrom = @fromAccount and t1.AccountTo = @toAccount
) -
( select ISNULL(SUM(Convert(DECIMAL(13,2),Amount)),0)
from TransactionDetail as t1
where t1.AccountFrom = @toAccount and t1.AccountTo = @fromAccount
)
)
select @credit
Run Code Online (Sandbox Code Playgroud)
输出应为十进制数字,如:13.56
但是,结果总是int,有什么建议吗?
默认scale值为0.如果要将结果作为特定格式,请尝试向变量显式添加精度和比例:
declare @credit decimal(13, 2) = (select . . .
Run Code Online (Sandbox Code Playgroud)
这种行为有很好的记录:
将存储在小数点右侧的小数位数.从p中减去此数字以确定小数点左侧的最大位数.可以存储在小数点右侧的最大小数位数.比例必须是从0到p的值.只有指定了precision时才能指定cSale.默认比例为0;