选择 19/12 返回 1,我需要十进制为 1.58 (sqlserver 2005)

2021-09-10 00:00:00 tsql sql-server
select 19/12 

返回 1,我需要的是 1.58 的十进制

return 1,I need in decimal that is 1.58

我使用的是 sqlserver 2005

I am using sqlserver 2005

推荐答案

您必须在 CAST 之前进行小数或浮点转换.否则就是整数除法

You have to do a decimal or float conversion before the CAST. Otherwise it is just integer division

SELECT
   CAST(1.00 * 19 / 12 AS DECIMAL(19,2))

但是,小数点后两位是演示问题.我会在客户端代码中这样做

However, the 2 decimal place thing is a presentation issue. I'd do that in the client code

相关文章