sql server 保留2位小数,如果整数 后面补0

比如:

数值 结果
15 15.00

13 13.00

13.2325 13.23

1、创建测试表,

create table test_num(id number, value number);

2、插入测试数据

insert into test_num values(1,15);

insert into test_num values(2,13);

insert into test_num values(3,13.2325);

insert into test_num values(4,15.7681);

commit;

3、查询表中数据,select t.*,rowid from test_num t;

4、编写sql,保留2位小数,如果整数 后面补0;

select t.*,

       case

         when not regexp_like(round(value, 2), '\D') then

          round(value, 2) || '.00'

         else

          to_char(round(value, 2))

       end as value2

  from test_num t;

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-01-06
select CAST(15 as decimal(18,2))
select CAST(15.235 as decimal(18,2))追问

后面没有补0

追答

你确定?

本回答被提问者采纳
第2个回答  2014-11-06
select 数值,convert(varchar,convert(decimal(10,2),数值)) as 结果 from table本回答被网友采纳
第3个回答  2014-11-06
CAST(数值 AS NUMERIC(12,2))追问

后面没有补0

追答

第4个回答  2014-11-06
加上decimal(X,2)

X代表非小数位的长度,2就是代表小数的位数
相似回答