在 MS SQL Server 中将数字格式化为百分比 [英] Format number as percent in MS SQL Server

查看:181
本文介绍了在 MS SQL Server 中将数字格式化为百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图简单地将数字格式化为带有两位小数的百分比.如果是 37 除以 38(又名 .973684210526315789),我希望它在 SQL 输出中显示 97.36%.我知道建议在应用程序中进行格式化,但这是为了自动导出.这是使用 SQL Server 2008.

I am trying to simply format a number as a percent with two decimal places. If it is 37 divided by 38 (aka .973684210526315789), I would like it to show 97.36 % in the SQL output. I know it is recommended to do formatting in the Application, however this is for an automated export. This is using SQL Server 2008.

这是我现在所拥有的:

select CONVERT(VARCHAR(50),cast(37 as decimal)/cast(38 as decimal)*100)+' %' AS [%]

如果您能在任何有用的函数中解释各种参数的含义.

If you could explain what the various parameters are as well in any function that would be helpful.

推荐答案

M.Ali的回答可以修改为

M.Ali's answer could be modified as

select Cast(Cast((37.0/38.0)*100 as decimal(18,2)) as varchar(5)) + ' %' as Percentage

这篇关于在 MS SQL Server 中将数字格式化为百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆