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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

Flowing Fount of Yak Knowledge

1433 Posts

Posted - 10/05/2012 :  07:00:21  Show Profile  Reply with Quote
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.

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;
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

Flowing Fount of Yak Knowledge

1433 Posts

Posted - 10/05/2012 :  07:27:53  Show Profile  Reply with Quote
Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000