我目前有一个非常大的查询,我缺少一个元素......我需要将数据切割成两个小数点(不需要舍入)。我用谷歌搜索,但无法适应当前的查询。
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
我基本上需要将信用卡和借记卡“案例”(就是你怎么说?)截断到两个小数点。我该怎么把它拉下来?提前谢谢!
答案 0 :(得分: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";
答案 1 :(得分:6)
CASTing将会轮回。如果你真的想截断你可以使用带有截断选项的ROUND,就像这样..
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'