[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1185
  • Last Modified:

Show SQL Calculation as Percentage

I have this code

(dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 80 AS sPercent

and when I run it it  sPercent shows as 0.

This is because the actual calcualtion is    12 / 80 = 0.15


so SQL rounds it off to 0


What I want to achieve is for sPercent to show 15%  


Can anyone advise me on this?

Thanks

SELECT     dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3 AS total_score, 
                      (dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 12 AS sPercent, 
                      dbo.vwSurveyCount.CompletedSurveys
FROM         dbo.tblBTLSurvey CROSS JOIN
                      dbo.vwSurveyCount

Open in new window

0
Ed
Asked:
Ed
4 Solutions
 
Ashish PatelCommented:
Use this

(dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 80.00 AS sPercent
0
 
Ashish PatelCommented:
I mean for 15 %
just do this
(dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 80*100 AS sPercent
Or or 15.00 %
(dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 80.00*100.00 AS sPercent


0
 
Ashish PatelCommented:
Your code below.
SELECT     dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3 AS total_score, 
                      (dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 12 * 100 AS sPercent, 
                      dbo.vwSurveyCount.CompletedSurveys
FROM         dbo.tblBTLSurvey CROSS JOIN
                      dbo.vwSurveyCount

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Jinesh KamdarCommented:
Try 12 / 8.0
0
 
Rajesh_mjCommented:
Hi,

/ (Divide) operator in Books online:

If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.

 
SELECT     dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3 AS total_score, 
                    (dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 0.12 AS sPercent, 
                      dbo.vwSurveyCount.CompletedSurveys
FROM         dbo.tblBTLSurvey CROSS JOIN
                      dbo.vwSurveyCount

Open in new window

0
 
jindalankushCommented:
during calculation convert any field  in float format
0
 
EdAuthor Commented:

The answer was


SELECT     dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3 AS total_score, 
                      CAST(CONVERT(decimal(18, 2), dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) 
                      / 80 AS varchar) + '%' AS sPercent, dbo.vwSurveyCount.CompletedSurveys
FROM         dbo.tblBTLSurvey CROSS JOIN
                      dbo.vwSurveyCount

Open in new window

0
 
EdAuthor Commented:
solution

SELECT     dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3 AS total_score,
                      CAST(CONVERT(decimal(18, 2), dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3)
                      / 80AS varchar) + '%' AS sPercent, dbo.vwSurveyCount.CompletedSurveys
FROM         dbo.tblBTLSurvey CROSS JOIN
                      dbo.vwSurveyCount
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now