Skip to content
Advertisement

SQL Server : round and add percent sign

I have the following SQL query that is returning a result of 92.967013425802 and I need it to be formatted like 93% and add the percent sign. I have tried changing the sum to round but I received an error

The function ’round’ is not a valid windowing function, and cannot be used with the OVER clause.

My query:

select 
    count(*) * 100.0 / sum(count(*)) over()
from 
    db_table_MetaData
group by 
    MetaValue
order by 
    MetaValue

Any help would be appreciated.

Advertisement

Answer

select 
    --Cast(Round(count(*) * 100.0 / sum(count(*)), 0) as nvarchar(5) + '%' 
      CAST(Round(count(*) * 100.0 / sum(count(*)), 0) as nvarchar(5)) + '%'
from 
    db_table_MetaData

This should do the trick.

In essence you take the 08/15 ROUND() function to get your numeric value. After this you cast it into a nvarchar(x) and add a string to your string. However I have no method of checking my syntax right now.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement