tags:

views:

1335

answers:

6

How can i convert this to a decimal in SQL? Below is the equation and i get the answer as 78 (integer) but the real answer is 78.6 (with a decimal) so i need to show this as otherwise the report wopnt tally up to 100%

(100 * [TotalVisit1]/[TotalVisits]) AS Visit1percent
+1  A: 

At least in MySQL (if it helps), if you want to use float numbers you had to use a type float field, not the regular int fields.

Ólafur Waage
+3  A: 

Try This:

(100.0 * [TotalVisit1]/[TotalVisits]) AS Visit1percent
Jason Lepack
Make sure you add formatting in your presentation layer to round that down to 1 or 2 decimal places. Otherwise, you're likely to run into things like 33.33333...
Michael Haren
A: 

Its probably overkill to do this, but you may wish to consider casting all values to floats to ensure accuracy at all phases.

(100.0 * ( [TotalVisit1]+0.0 )/([TotalVisits]+0.0) ) as Visit1percent

Note, you really need to put code in here for the case that TotalVisits == 0 or you will get a division by 0 answer.

Kent Fredric
A: 

convert(decimal(5,2),(100 * convert(float,[TotalVisit1])/convert(float,[TotalVisits]))) AS Visit1percent

Ugly, but it works.

Shawn Simon
Thanks this is the one ive gone with as it rounded up the number, the other answers all worked as well though thanks allot every one :)
I've found it better to convert to decimal than float. Certain divisions work better with decimal, at least on Microsoft SQL server:SELECT 5555/CAST(1000 AS float), CAST(5555/CAST(1000 AS float) AS numeric(5, 2)), 5555/CAST(1000 AS decimal), CAST(5555/CAST(1000 AS decimal) AS Numeric(5, 2))
A: 

Just add a decimal to the 100

(100.0 * [TotalVisit1]/[TotalVisits]) AS Visit1percent

this forces all processing to happen in floats... if you want the final output as text, and truncated for display to only one decimal place, use Str function

Str( 100.0 * [TotalVisit1]/[TotalVisits], 4, 1 ) AS Visit1percent
Charles Bretana
Like your str answer nice and neat looking :)
Thank you sir! ...glad to help
Charles Bretana
or thank you miss ;)
A: 

This might not address you issue directly, but when you round a set of numbers for display you're never guaranteed to get numbers that add to 100 unless you take special precautions. For example, rounding 33.33333, 33.33333 and 33.33333 is going to leave you one short on the sum, so the logical thing to do is to modify the percentage for the largest value in the set to take account of any difference.

Here's a way of doing that in Oracle SQL using analytic functions and a subquery factoring (WITH) clause to generate sample data.

with data as (select 25 num from dual union all
              select 25     from dual union all
              select 25     from dual)
select num, 
case
   when rnk = 1
   then 100 - sum(pct) over (order by rnk desc
                             rows between unbounded preceding
                                      and         1 preceding)
   else pct
end pct
from
   (
   select num,
          round(100*ratio_to_report(num) over ()) pct,
          row_number() over (order by num desc) rnk
   from data
   )
/


NUM                    PCT                    
---------------------- ---------------------- 
25                     33                    
25                     33                     
25                     34
David Aldridge
This is a bad idea in general. If the numbers don't add to 100 due to rounding, then that's how it is. Monkeying around with them is just going to give less accurate results.
It's neither a good nor a bad idea, and generalities don't come into it. It's just a technique for achieving a specific hypothetical requirement.
David Aldridge
True, but it's a bad requirement. It leads to incorrectly displayed data. And it doesn't answer the question that was posed above. And it's not the most accurate solution to making the percents add to 100 either, though it may be the simplest.