1

I am trying to get the percent and it just shows up as zero. I want to show two decimal places such as 0.65 Here is a piece of the query I am selecting:

select count(numbers)/count(othernumbers) decimal(3,2) as"rate"

if I use this it shows up as 0 and gets rid of the rest

select count(numbers)/count(othernumbers) as"rate"

5 Answers 5

3

need to convert both of your "count(numbers)" and "count(othernumbers)" to decimal also.

select  convert(decimal(5,2), count(numbers))
        /
        convert(decimal(5,2), count(othernumbers))
        as"rate"

Here's an example that works in SSMS (Sql Server):

select Convert(decimal(3,2), convert(decimal(4,2), 1.0) / convert(decimal(4,2), 10.0)) as [rate]
4
  • It doesn't in my SSMS: select 1/10 as [rate]
    – ganders
    Apr 16, 2015 at 14:17
  • 1
    Since SQL server isn't MySQL. Apr 16, 2015 at 14:17
  • Arithmetic overflow error converting int to data type numeric.
    – bigdowg
    Apr 16, 2015 at 14:34
  • See my second example, it's because of converting 10 to decimal(3,2), which says only 1 digit left of the decimal, and 10 has 2 digits
    – ganders
    Apr 16, 2015 at 14:54
2

You have to use this convert value to Decimal, This will give you the decimal till two places

SELECT CONVERT( DECIMAL(10,2),
                    ( CONVERT(DECIMAL(10,2), numbers) / 
                      CONVERT(DECIMAL(10,2), othernumbers) ) ) AS rate
3
  • you dont even use any column names in this what the?
    – bigdowg
    Apr 16, 2015 at 14:26
  • numbers & other numbers is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
    – bigdowg
    Apr 16, 2015 at 14:32
  • @bigdowg:- you have to use two number as you required instead of numbers & othernumbers. this is just sample way to the query. If you have issue to implement in your way, show your full query, so that we can resolve that Apr 16, 2015 at 16:22
0
SELECT FORMAT(this / that, 2) as rate

Meanwhile, I question whether COUNT(numbers) is what you want. That counts how many rows have a non-NULL value in the column numbers.

Also, fraction is usually more like x / (x+y) -- meaning the fraction of the total (x+y) that is x.

A "percentage" needs 100* somewhere. 13/20 is the fraction 0.65 or the percentage 65.00 .

0

Have you checked ceil(), floor() and round()

If you want without rounding anything

SELECT TRUNCATE(count(numbers)/count(othernumbers),2) as "rate"

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_truncate

1
  • Oh sorry , I saw the mysql tag that's why Apr 16, 2015 at 15:16
0
select convert(decimal(11, 2), 102)

select convert(decimal(11, 2), 102.5)

select convert(decimal(11, 2), 102.74)

select convert(decimal(11, 2), 102.745)

Results: 102.00; 102.50; 102.74; 102.75

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.