SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1418 Posts

Posted - 10/05/2012 :  07:00:21  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/05/2012 :  07:06:41  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

1418 Posts

Posted - 10/05/2012 :  07:27:53  Show Profile  Reply with Quote
thanks
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000