sqlsql-servermathprocedure

Getting decimal value from division


I am trying to get a decimal value with the code:

declare @percents decimal(16, 2) = convert(decimal(16, 2), 100);

declare @fifteenPercent decimal(16,2) = convert(decimal(16,2), @fifteen) * @percents / convert(decimal(16, 2), @total);

Both @total and @fifteen variables are integers. What I get is a whole number in following select

select 
    '1-15: (' + trim(str(@fifteen)) +') ' + trim(str(@fifteenPercent)) + '%' as ratingB

I get 1-15: (38230) 7%

I want 1-15: (38230) 7.28%

I converted all variables to decimal(16,2) and still get an integer value


Solution

  • thank you for suggestions. I found the answer myself The point is to really use FORMAT() funciton, but the percentage needs to be divided by 100, before formating.

    format(@fiteenPercentage / 100, 'P2')
    

    If divided, the outcome is

    26-50: (343474) 3,37 %

    If not the outcome is

    26-50: (343474) 337,00 %

    which means the function needs to work with number between 0 and 1