Sql 保留两位小数

数据大师
数据大师
数据大师
294
文章
0
评论
2021-06-3011:29:19 评论 3,398 802字
摘要

SQL中字段保留两位小数需要使用 Round() 函数,本文讲述Round() 函数的用法

一、SQL中字段保留两位小数:

1. 使用 Round() 函数,如 Round(number,2) ,其中参数2表示保留两位有效数字,但是只负责四舍五入到两位小数,但是不负责截断例如 ROUND(3.141592653, 2) 结果为3.140000000;2. 使用 Convert(decimal(10,2),number) 实现转换,其中参数2表示保留两位有效数字 例如Convert(decimal(10,2),3.1415) 结果为3.14;3. 使用 cast(number as decimal(10,2)) 实现转换,其中参数2表示保留两位有效数字 例如cast(3.1415 as decimal(10,2)) 结果为3.14;

备注:CAST与CONVERT都可以执行数据类型转换,且都默认实现了四舍五入

 

如果目标表的字段是decimal(10,4)型的,从源表查数据:select round(field,2) from sourcetable;sql查询的结果字段的小数位可能是2位,但是该数据插入目标表,字段的小数位数就是4位后面2位以0补充;select decimal(field,10,2) from sourcetable;该数据插入目标表,字段的小数位就是2位

 

二、SQL中两个字段相除并加上%

 

select decimal(field1/field2*100,10,2) || "%" from tablename;select round(field1/field2*100,2) || "%" from tablename;select concat(round(field1/field2*100,2) ,"%") from tablename;

 

End.

作者:hyfstyle

转载如果涉及作品问题请联们第一时间删除(微信lovedata0520

更多文章前往首页浏览http://www.itongji.cn/

  • 我的微信公众号
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: