Please start any new threads on our new site at http://forums.sqlteam.com. 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 Register Now and get your question answered!
 All Forums  General SQL Server Forums  New to SQL Server Programming  Rounding, convert to percent Reply to Topic  Printer Friendly
Author  Topic

theKid27
Starting Member

21 Posts

 Posted - 09/26/2013 :  09:14:13 Hi Experts/Members, I have a question bout rounding and converting to percentage and adding in the '%' This is my original codeSELECT * FROM ( SELECT Baby, CAST( CAST( SUM(TotalBaby) AS DECIMAL )/ CAST( SUM(TotalParent) AS DECIMAL) AS DECIMAL(10,4)) as BabyValue FROM NewBorn WHERE Category = 'Boy' AND TotalParent > 0 GROUP BY size )returnResult PIVOT ( SUM(BabyValue)FOR size IN ( [A],[B], [C],[D] ) ) AS BabypivotResult The output is as below: A B C D0.0022 0.0029 0.0011 0.0032I need to change it to percentage so i put *100 as below, so I get the data as below CAST( CAST( SUM(TotalBaby) AS DECIMAL )/ CAST( SUM(TotalParent) AS DECIMAL) AS DECIMAL(10,4)) * 100 as BabyValueA B C D0.2200 0.2900 0.1100 0.3200But I also need my data to have the '%' and it should have 2 decimal place which is as below, I have tried to make it this way CAST( CAST( SUM(TotalBaby) AS DECIMAL )/ CAST( SUM(TotalParent) AS DECIMAL) AS DECIMAL(10,4)) * 100 + '%' as BabyValueBut it prompt me the error 'Error converting data type varchar to numeric.'A B C D0.22% 0.29% 0.11% 0.32%Appreciate if anyone of you could advise me or share me some ideas. Thank you.

James K
Flowing Fount of Yak Knowledge

3873 Posts

 Posted - 09/26/2013 :  09:36:26 If you want to append a percent symbol to the end of a numeric quantity, you have to convert the numeric quantity to be of char or varchar type.Usually this is not advisable to do this type of thing in the database. It is much better to do it in the presentation layer such as reporting services or a GUI where you are consuming the data if you have one. If you absolutely must do it in SQL, then convert it to varchar using cast, convert or: `STR( 100.0* SUM(TotalBaby)/SUM(TotalParent) ,10,2)+'%'` Edited by - James K on 09/26/2013 09:36:50

theKid27
Starting Member

21 Posts

 Posted - 09/26/2013 :  18:54:16 Thanks James K for the advice, I'll put it at the GUI.
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC