Desplay Results as a percentage

I have Script that outputs a percentage value in the results, Purchase_Percent_2014 and YTD_Purchase_Percent_2015 currently they go out 6 decimal places I would like it to display just 1 decimal place with the % symbol for example 22.5% currently it is displaying 22.500000


SELECT        VPP.Vendor_ID, VPP.Vendor_Name, VPP.Purchase_Ammount_2013, VPP.Purchase_Ammount_2014, VPP.YTD_Purchase_Ammount_2015, ISNULL(VPP.Purchase_Percent_2014, 0) AS Purchase_Percent_2014, 
ISNULL(VPP.YTD_Purchase_Percent_2015, 0) AS YTD_Purchase_Percent_2015
FROM            (SELECT        Vendor_ID, Vendor_Name, SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2013, SUM(CASE WHEN ([Year]) 
= 2014 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2014, SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) AS YTD_Purchase_Ammount_2015, 
(SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) 
= 2013 THEN Purchase_Ammount ELSE 0 END), 0) AS Purchase_Percent_2014, (SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) 
= 2014 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END), 0) AS YTD_Purchase_Percent_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING         (Class_ID IN ('MAN', 'VEN'))) AS VPP LEFT OUTER JOIN
(SELECT        CUSTNMBR, CUSTCLAS
FROM            RM00101
WHERE        (CUSTCLAS IN ('B', 'CF', 'COLLECTIONS', 'EMP', 'FA', 'OBS', 'PA', 'PD', 'SA', 'SD'))) AS RM ON VPP.Vendor_ID = RM.CUSTNMBR
WHERE        (RM.CUSTNMBR IS NULL)
ORDER BY VPP.Vendor_ID

Open in new window

skull52IT director Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
Crazy query :)

Round



CONTACT
lcohanDatabase AnalystCommented:
SELECT        VPP.Vendor_ID, VPP.Vendor_Name, VPP.Purchase_Ammount_2013, VPP.Purchase_Ammount_2014, VPP.YTD_Purchase_Ammount_2015,
      --ISNULL(VPP.Purchase_Percent_2014, 0) AS Purchase_Percent_2014,
      CONVERT(VARCHAR(50),ISNULL(VPP.Purchase_Percent_2014, 0)+' %' AS Purchase_Percent_2014,
      --ISNULL(VPP.YTD_Purchase_Percent_2015, 0) AS YTD_Purchase_Percent_2015
      CONVERT(VARCHAR(50),ISNULL(VPP.YTD_Purchase_Percent_2015, 0)+' %' AS YTD_Purchase_Percent_2015
FROM            (SELECT        Vendor_ID, Vendor_Name, SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2013, SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2014, SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) AS YTD_Purchase_Ammount_2015,
(SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year])
= 2013 THEN Purchase_Ammount ELSE 0 END), 0) AS Purchase_Percent_2014, (SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END), 0) AS YTD_Purchase_Percent_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING         (Class_ID IN ('MAN', 'VEN'))) AS VPP LEFT OUTER JOIN
(SELECT        CUSTNMBR, CUSTCLAS
FROM            RM00101
WHERE        (CUSTCLAS IN ('B', 'CF', 'COLLECTIONS', 'EMP', 'FA', 'OBS', 'PA', 'PD', 'SA', 'SD'))) AS RM ON VPP.Vendor_ID = RM.CUSTNMBR
WHERE        (RM.CUSTNMBR IS NULL)
ORDER BY VPP.Vendor_ID
skull52IT director Author Commented:
Najam,
Yeah... I know it is a bit out there, but it works. I should have said I am on MSSQL 2008R2 so I can't use CONCAT
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Najam UddinCommented:
SELECT VPP.Vendor_ID, VPP.Vendor_Name, ROUND(VPP.Purchase_Ammount_2013,2), ROUND(VPP.Purchase_Ammount_2014,2), ROUND(VPP.YTD_Purchase_Ammount_2015,2),
      CONVERT(VARCHAR(50),ROUND(ISNULL(VPP.Purchase_Percent_2014, 0),2)+' %' AS Purchase_Percent_2014,
      CONVERT(VARCHAR(50),ROUND(ISNULL(VPP.YTD_Purchase_Percent_2015, 0),2)+' %' AS YTD_Purchase_Percent_2015
FROM          
 (SELECT  Vendor_ID, Vendor_Name
, SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2013
, SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2014
, SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) AS YTD_Purchase_Ammount_2015
, (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END), 0) AS Purchase_Percent_2014
, (SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END), 0) AS YTD_Purchase_Percent_2015
FROM            
SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING         (Class_ID IN ('MAN', 'VEN'))) AS VPP LEFT OUTER JOIN
(SELECT        CUSTNMBR, CUSTCLAS
FROM            RM00101
WHERE        (CUSTCLAS IN ('B', 'CF', 'COLLECTIONS', 'EMP', 'FA', 'OBS', 'PA', 'PD', 'SA', 'SD'))) AS RM ON VPP.Vendor_ID = RM.CUSTNMBR
WHERE        (RM.CUSTNMBR IS NULL)
ORDER BY VPP.Vendor_ID
skull52IT director Author Commented:
lcohan,
I get the following error
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
lcohanDatabase AnalystCommented:
sorry missed the ROUND - added with 2 decimals instead of one but you can easily change it to 1 only:


SELECT        VPP.Vendor_ID, VPP.Vendor_Name, VPP.Purchase_Ammount_2013, VPP.Purchase_Ammount_2014, VPP.YTD_Purchase_Ammount_2015,
      --ISNULL(VPP.Purchase_Percent_2014, 0) AS Purchase_Percent_2014,
      CONVERT(VARCHAR(50),ISNULL(cast(round(VPP.YTD_Purchase_Percent_2014,2) as numeric(36,2)), 0))+' %' AS Purchase_Percent_2014,
      --ISNULL(VPP.YTD_Purchase_Percent_2015, 0) AS YTD_Purchase_Percent_2015
      CONVERT(VARCHAR(50),ISNULL(cast(round(VPP.YTD_Purchase_Percent_2015,2) as numeric(36,2)), 0))+' %' AS YTD_Purchase_Percent_2015
FROM            (SELECT        Vendor_ID, Vendor_Name, SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2013, SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2014, SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) AS YTD_Purchase_Ammount_2015,
(SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year])
= 2013 THEN Purchase_Ammount ELSE 0 END), 0) AS Purchase_Percent_2014, (SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END), 0) AS YTD_Purchase_Percent_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING         (Class_ID IN ('MAN', 'VEN'))) AS VPP LEFT OUTER JOIN
(SELECT        CUSTNMBR, CUSTCLAS
FROM            RM00101
WHERE        (CUSTCLAS IN ('B', 'CF', 'COLLECTIONS', 'EMP', 'FA', 'OBS', 'PA', 'PD', 'SA', 'SD'))) AS RM ON VPP.Vendor_ID = RM.CUSTNMBR
WHERE        (RM.CUSTNMBR IS NULL)
ORDER BY VPP.Vendor_ID
skull52IT director Author Commented:
Najam,

Same Error

Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
lcohanDatabase AnalystCommented:
one more try...sorry harder to write it without being able to actually run it...


SELECT        VPP.Vendor_ID, VPP.Vendor_Name, VPP.Purchase_Ammount_2013, VPP.Purchase_Ammount_2014, VPP.YTD_Purchase_Ammount_2015,
      --ISNULL(VPP.Purchase_Percent_2014, 0) AS Purchase_Percent_2014,
      cast(ISNULL(cast(round(VPP.YTD_Purchase_Percent_2014,2) as numeric(36,2)), 0) as VARCHAR(50)) +' %' AS Purchase_Percent_2014,
      --ISNULL(VPP.YTD_Purchase_Percent_2015, 0) AS YTD_Purchase_Percent_2015
      cast(ISNULL(cast(round(VPP.YTD_Purchase_Percent_2015,2) as numeric(36,2)), 0) as VARCHAR(50)) +' %' AS Purchase_Percent_2015
FROM            (SELECT        Vendor_ID, Vendor_Name, SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2013, SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2014, SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) AS YTD_Purchase_Ammount_2015,
(SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year])
= 2013 THEN Purchase_Ammount ELSE 0 END), 0) AS Purchase_Percent_2014, (SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END), 0) AS YTD_Purchase_Percent_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING         (Class_ID IN ('MAN', 'VEN'))) AS VPP LEFT OUTER JOIN
(SELECT        CUSTNMBR, CUSTCLAS
FROM            RM00101
WHERE        (CUSTCLAS IN ('B', 'CF', 'COLLECTIONS', 'EMP', 'FA', 'OBS', 'PA', 'PD', 'SA', 'SD'))) AS RM ON VPP.Vendor_ID = RM.CUSTNMBR
WHERE        (RM.CUSTNMBR IS NULL)
ORDER BY VPP.Vendor_ID

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skull52IT director Author Commented:
lcohan,
Perfect, Thanks
Scott PletcherSenior DBACommented:
SELECT        VPP.Vendor_ID, VPP.Vendor_Name, VPP.Purchase_Ammount_2013, VPP.Purchase_Ammount_2014,
              VPP.YTD_Purchase_Ammount_2015,
              ISNULL(CAST(VPP.Purchase_Percent_2014 AS varchar(6)) + '%', '0') AS Purchase_Percent_2014,
              ISNULL(CAST(VPP.YTD_Purchase_Percent_2015 AS varchar(6)) + '%', '0') AS YTD_Purchase_Percent_2015
FROM            (SELECT        Vendor_ID, Vendor_Name, SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2013, SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2014, SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) AS YTD_Purchase_Ammount_2015,
CAST((SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year])
= 2013 THEN Purchase_Ammount ELSE 0 END), 0) AS decimal(5, 2)) AS Purchase_Percent_2014, CAST((SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END), 0) AS decimal(5, 2)) AS YTD_Purchase_Percent_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING         (Class_ID IN ('MAN', 'VEN'))) AS VPP LEFT OUTER JOIN
(SELECT        CUSTNMBR, CUSTCLAS
FROM            RM00101
WHERE        (CUSTCLAS IN ('B', 'CF', 'COLLECTIONS', 'EMP', 'FA', 'OBS', 'PA', 'PD', 'SA', 'SD'))) AS RM ON VPP.Vendor_ID = RM.CUSTNMBR
WHERE        (RM.CUSTNMBR IS NULL)
ORDER BY VPP.Vendor_ID
lcohanDatabase AnalystCommented:
phew...and as said if you really want only one decimal in the percentage(I left 2 as I believe this is "regular" way to display percentages) you can change the

round(VPP.YTD_Purchase_Percent_2014,2) as numeric(36,2)

like

round(VPP.YTD_Purchase_Percent_2014,2) as numeric(36,1)

to get only one decimal place.
skull52IT director Author Commented:
Yeah... I know this was a twisted script, but I was given little time to do it.  That's why I had a hard time figuring out how to do the conversions, I knew i could use round in there but wasn't sure of the syntax and the concatenation of the % symbol, thanks again.
skull52IT director Author Commented:
Scott,
Thanks for your input I tried you code and it generated the following Error  
Msg 8115, Level 16, State 5, Line 1
Arithmetic overflow error converting numeric to data type varchar.
Scott PletcherSenior DBACommented:
Sorry, would need to increase the ranges, didn't expect you to have that type of % gains :-):

SELECT        VPP.Vendor_ID, VPP.Vendor_Name, VPP.Purchase_Ammount_2013, VPP.Purchase_Ammount_2014,
              VPP.YTD_Purchase_Ammount_2015,
              ISNULL(CAST(VPP.Purchase_Percent_2014 AS varchar(20)) + '%', '0') AS Purchase_Percent_2014,
              ISNULL(CAST(VPP.YTD_Purchase_Percent_2015 AS varchar(20)) + '%', '0') AS YTD_Purchase_Percent_2015
FROM            (SELECT        Vendor_ID, Vendor_Name, SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2013, SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END) AS Purchase_Ammount_2014, SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) AS YTD_Purchase_Ammount_2015,
CAST((SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year]) = 2013 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year])
= 2013 THEN Purchase_Ammount ELSE 0 END), 0) AS decimal(17, 2)) AS Purchase_Percent_2014, CAST((SUM(CASE WHEN ([Year]) = 2015 THEN Purchase_Ammount ELSE 0 END) - SUM(CASE WHEN ([Year])
= 2014 THEN Purchase_Ammount ELSE 0 END)) * 100 / NULLIF (SUM(CASE WHEN ([Year]) = 2014 THEN Purchase_Ammount ELSE 0 END), 0) AS decimal(17, 2)) AS YTD_Purchase_Percent_2015
FROM            SSG_Vendor_Yearly_Totals
GROUP BY Vendor_ID, Vendor_Name, Class_ID
HAVING         (Class_ID IN ('MAN', 'VEN'))) AS VPP LEFT OUTER JOIN
(SELECT        CUSTNMBR, CUSTCLAS
FROM            RM00101
WHERE        (CUSTCLAS IN ('B', 'CF', 'COLLECTIONS', 'EMP', 'FA', 'OBS', 'PA', 'PD', 'SA', 'SD'))) AS RM ON VPP.Vendor_ID = RM.CUSTNMBR
WHERE        (RM.CUSTNMBR IS NULL)
ORDER BY VPP.Vendor_ID
skull52IT director Author Commented:
No problem Scott, I should have caught that myself, yeah we do have some percentage swings.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.