7

目前我有一个相当大的查询,我缺少一个元素......我需要将数据减少到小数点后两位(不需要四舍五入)。我用谷歌搜索,但无法将其适应当前查询。

SELECT 
    FechaDeSistema, Code, 
    CASE 
        WHEN DR_Socio = @SocioNum THEN Cantidad 
    END as Debit, 
    CASE 
        WHEN CR_Socio = @SocioNum THEN Cantidad 
    END AS Credit, 
    CASE 
        WHEN DR_Socio = @SocioNum THEN BalanceDebito 
        WHEN CR_Socio = @SocioNum THEN BalanceCredito 
    END AS Balance 
FROM 
    Ledger 
WHERE 
    (Debito_Cuenta = @Acct) OR 
    (Credito_Cuenta = @Ncct) 
ORDER BY 
    FechaDeSistema DESC

我基本上需要将贷记和借记“案例”(您是这么说的吗?)截断到小数点后两位。我怎么能把它拉下来?提前致谢!

4

2 回答 2

8

铸造将圆。如果你真的想截断你可以使用 ROUND 与 truncate 选项,像这样..

DECLARE @MyNum DECIMAL(19,4) = 100.1294

SELECT @MyNum 'MyNum'
, CAST(@MyNum AS DECIMAL(19,2)) 'Cast will round'
, ROUND(@MyNum,2,4) 'Round with truncate option'
, CAST(ROUND(@MyNum,2,4) AS DECIMAL(19,2)) 'Better Truncate'
于 2013-07-12T20:31:52.427 回答
7

只需将其转换为较低精度的小数,这将切断尾随数字。

cast(yourColumnName as decimal(19,2)) as desiredColumnName

更具体地说是您的查询代码

SELECT 
    FechaDeSistema, 
    Code, 
    CASE 
        WHEN DR_Socio = @SocioNum THEN cast(Cantidad as decimal(19,2)) 
    END as Debit, 
    CASE 
        WHEN CR_Socio = @SocioNum THEN cast(Cantidad as decimal(19,2)) 
    END AS Credit, 
    CASE 
        WHEN DR_Socio = @SocioNum THEN cast(BalanceDebito as decimal(19,2)) 
        WHEN CR_Socio = @SocioNum THEN cast(BalanceCredito as decimal(19,2)) 
    END AS Balance 
FROM Ledger 
WHERE (Debito_Cuenta = @Acct) 
OR    (Credito_Cuenta = @Ncct) 
ORDER BY FechaDeSistema DESC";
于 2013-07-12T15:40:50.400 回答