Solved

Format to Percentage in SQL Server Query

Posted on 2016-10-04
8
184 Views
Last Modified: 2016-10-04
I am trying to format my result to a percentage with 0 decimals. This is what I have for the formatting/SQL syntax. Can anyone help me reset this to work correctly? Right now when the results are displayed it shows this for an example: 100.00000000%

CASE WHEN (jodrtg.fnqty_comp / jodrtg.foperqty) * 100 > 0 AND jodbom.fqty_iss = '0' AND joitem.fprodcl = '01' THEN ((Round(jodrtg.fnqty_comp, 0) / Round(jodrtg.foperqty, 0)) * Round(100, 0)) * - 1 ELSE (Round(jodrtg.fnqty_comp, 1) / Round(jodrtg.foperqty, 0)) * Round(100, 0) END * 1) + '%' AS [% CMP]

Open in new window

0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41828001
you can try to cast as an integer;

cast(CASE WHEN (jodrtg.fnqty_comp / jodrtg.foperqty) * 100 > 0 AND jodbom.fqty_iss = '0' AND joitem.fprodcl = '01' THEN ((Round(jodrtg.fnqty_comp, 0) / Round(jodrtg.foperqty, 0)) * Round(100, 0)) * - 1 ELSE (Round(jodrtg.fnqty_comp, 1) / Round(jodrtg.foperqty, 0)) * Round(100, 0) END * 1) as integer) + '%' AS [% CMP]

Open in new window

0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 41828009
Use this:
cast(cast(round(
		CASE 
			WHEN (jodrtg.fnqty_comp / jodrtg.foperqty) * 100 > 0 AND jodbom.fqty_iss = '0' AND joitem.fprodcl = '01' 
				THEN  
					-(jodrtg.fnqty_comp / jodrtg.foperqty *100.0)
			ELSE 
				jodrtg.fnqty_comp /jodrtg.foperqty * 100.0
		END
	,0) as int) as varchar(5)) + '%' AS [% CMP]

Open in new window

Be aware that I edited this post!
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41828036
Neither of those seem to be working. I am getting a syntax errors when I try to execute my query.

Error in list of function arguments: 'AS' not recognized.
Error in list of function arguments: ',' not recognized.
Unable to parse query text.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
ID: 41828047
you have an extra )

try this:
CAST(CAST(CASE 
WHEN (jodrtg.fnqty_comp / jodrtg.foperqty) * 100 > 0 AND jodbom.fqty_iss = '0' AND joitem.fprodcl = '01' 
THEN ((Round(jodrtg.fnqty_comp, 0) / Round(jodrtg.foperqty, 0)) * Round(100, 0)) * - 1 
ELSE (Round(jodrtg.fnqty_comp, 1) / Round(jodrtg.foperqty, 0)) * Round(100, 0) 
END * 1 AS INT) AS VARCHAR) + '%' 
AS [% CMP]

Open in new window

0
 
LVL 27

Accepted Solution

by:
Zberteoc earned 250 total points
ID: 41828052
I mentioned that I edited my post because it had errors. I will post it again here:
cast(cast(round(
		CASE 
			WHEN (jodrtg.fnqty_comp / jodrtg.foperqty) * 100 > 0 AND jodbom.fqty_iss = '0' AND joitem.fprodcl = '01' 
				THEN  
					-(jodrtg.fnqty_comp / jodrtg.foperqty *100.0)
			ELSE 
				jodrtg.fnqty_comp /jodrtg.foperqty * 100.0
		END
	,0) as int) as varchar(5)) + '%' AS [% CMP]

Open in new window

0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 41828054
Eric's post will not round the values correctly. You have to use ROUND before casting to int.
select 100.0/6, cast(100.0/6 as int), cast(round(100.0/6, 0) as int)

--------------------------------------- ----------- -----------
                              16.666666          16          17

Open in new window

0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 41828074
Actually he did use round but twice inside the case branches, which could be multiple. I used it only once wrapping the whole case statement, which I think is the better way.
0
 
LVL 1

Author Closing Comment

by:Lawrence Salvucci
ID: 41828121
Thank you both for all your help. If I could give you both BEST solution, I would but it only lets you choose one.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question