Solved

Format to Percentage in SQL Server Query

Posted on 2016-10-04
8
27 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 69

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 26

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
 
LVL 69

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 26

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 26

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 26

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
INSERT INTO SELECT JOIN THING 2 27
separate column 24 21
SSIS import multiple CSVs into associated tables 3 32
Stored Procedure 2 10
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now