Format number as percent in MS SQL Server

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.

Here is what I have now:

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.

Answers


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

In SQL Server 2012 and later, there is the FORMAT() function. You can pass it a 'p' parameter for percentage. For example:

SELECT FORMAT((37.0/38.0),'p') as [Percentage]

SELECT cast( cast(round(37.0/38.0,2) AS DECIMAL(18,2)) as varchar(100)) + ' %'

RESULT:  0.97 %

Need Your Help

Is there a way to access the Trailer headers in a chunked-enconded response in .Net 4.0?

.net-4.0 httpwebrequest http-headers chunked-encoding

Using HttpWebRequest/Response, and the Trailer headers in the chunked-encoded response are being thrown away (I've actually stepped through the .Net 4.0 reference source to see where it calls

Emacs: global key rebinding unless the key has a special meaning

emacs key-bindings

I would like to globally change the usual binding of a key in Emacs. For instance, Return runs the command newline by default, and I would like it to run electric-newline-and-maybe-indent instead. I

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.