如何将SQL输出修复为两位小数

时间:2016-05-10 05:56:33

标签: sql sql-server decimal

我有这个SELECT语句:

int r =  Random.value*n;

其中显示输出列为十进制数字(例如)62.24837411,我知道我可以将其放入excel并将列格式化为百分比,但我的问题是如何让SQL将其转换为百分比?我已经尝试将十进制(18,2)更改为十进制(4,2)这是我想要的但它只是抛出数学溢出错误?

3 个答案:

答案 0 :(得分:0)

试试这个:

select  convert(DECIMAL(10,2),column)

精确到2位小数

答案 1 :(得分:0)

您将cast放在错误的地方

cast(SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END) as decimal(18,2))/cast(Count(t.clmCaseID) as decimal(18,2))*100 as [TotalResolved %],

CAST放在整个结果中,如下所示:

cast(SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END)/cast(Count(t.clmCaseID) as decimal(18,2))*100  as decimal(18,2)) as [TotalResolved %],

答案 2 :(得分:0)

AS Arul建议,您需要在整个列计算中使用强制转换函数。

  SELECT
 t.clmUnit,
 t.clmConType,
 Count(t.clmCaseID) TotalCases,
 SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END) TotalResolved,
 cast(SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END))/Count(t.clmCaseID) * 100  as decimal(18,2)) as [TotalResolved %],
SUM(CASE WHEN t.clmOutcomeType='Unresolved' THEN 1 ELSE 0 END) TotalCaseProgressed,
cast(SUM(CASE WHEN t.clmOutcomeType='Unresolved' THEN 1 ELSE 0 END))/Count(t.clmCaseID) * 100 as decimal(18,2)) as [TotalCaseProgressed %],
SUM(CASE WHEN t.clmOutcomeType='Work In Progress' THEN 1 ELSE 0 END) TotalFurtherConReq,
cast(SUM(CASE WHEN t.clmOutcomeType='Work In Progress' THEN 1 ELSE 0 END))/Count(t.clmCaseID) * 100 as decimal(18,2)) as [TotalFurtherConReq %]