Solved

Show SQL Calculation as Percentage

Posted on 2007-11-23
8
1,171 Views
Last Modified: 2010-04-21
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
Comment
Question by:edjones1
8 Comments
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 300 total points
ID: 20338133
Use this

(dbo.tblBTLSurvey.NewBusinessQ1 + dbo.tblBTLSurvey.NewBusinessQ2 + dbo.tblBTLSurvey.NewBusinessQ3) / 80.00 AS sPercent
0
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 300 total points
ID: 20338136
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
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 300 total points
ID: 20338138
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
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20338140
Try 12 / 8.0
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 20338166
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
 
LVL 4

Accepted Solution

by:
jindalankush earned 200 total points
ID: 20338256
during calculation convert any field  in float format
0
 

Author Comment

by:edjones1
ID: 20338305

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
 

Author Closing Comment

by:edjones1
ID: 31410648
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

867 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

15 Experts available now in Live!

Get 1:1 Help Now