MS SQL numeric 数据类型和去除零的转换

MS SQL numeric data type and convertion with removing zero

create table test_int
    (
     num bigint
    )

insert into test_int (num)
values (4096);

我的任务是计算 4096/1024/1024 并得到十进制答案。好的,int 不在点之后存储值,所以:

select CAST (num as decimal)/1024/1024 AS decimal, ROUND ((CAST (num as decimal)/1024/1024),4,1) AS numeric  from test_int

第一个是纯结果,第二个是四舍五入后的结果:

decimal         numeric
0.00390625000 0.00390000000

任务是删除值后的空零。

select convert(decimal(25,5), 4096/1024/1024 ,0) 

returns 0.00000.

那么我怎样才能得到 0.0039 而不是 0.00390000000? 在此先感谢您提供任何帮助。

将结果转换为 FLOAT

查询

SELECT
CAST
(
    CAST(num as decimal)/1024/1024 
    AS FLOAT
) AS decimal, 
CAST
(
    ROUND((CAST (num as decimal)/1024/1024),4,1) 
    AS FLOAT
) AS numeric  
from test_int;

结果

+---------------+---------------+
| decimal       |   numeric     |
+---------------+---------------+
| 0.00390625    | 0.0039        |
+---------------+---------------+

SQL Fiddle

这个可以用,但如果我没记错的话,这只对 2008 及以上版本有效。

SELECT CONVERT(DOUBLE PRECISION, 0.00390000000)

result: 0.0039