Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 function

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-10-05 : 07:00:21
Hi,
How can I have a function so that you can pass a number to it, and the function expects one parameter i.e.:
@dp tinyint

so then the function does something like the following:

takes the passed in number i.e. 0.199786581197469 and then returns its percentage with @dp decimal places, therefore the answer will be: 19.98%
Please note that the functioni should return the percentage sign too, so I guess it should return varchar.
Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-05 : 07:06:41
You can do it like in the example below:
DECLARE @x FLOAT = 0.199786581197469;
SELECT CAST(CAST(100*@x AS DECIMAL(19,2)) AS VARCHAR(32))+'%';
Most experts would advise that you don't create a function to do this - rather, just do it in-line, for better performance.

Also, in general, you would want to do the formatting and adding percentage symbols etc. in the presentation layer (such as a client application or reporting services) if you have one, rather than doing it in the SQL query.
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-10-05 : 07:27:53
thanks
Go to Top of Page
   

- Advertisement -