question

sqlcraz avatar image
sqlcraz asked

Unable to get decimals and percentages

Any help is appreciated. I would like the values to show 0.0%. I tried all the following methods but getting error messages: •STR(ROUND(([Future]*100.0/Total), 2) as decimal(5,1)) + '%' as 'Percentage' •Incorrect syntax near the keyword 'AS' •cast(ROUND(([Future]*100.0/Total), 2) as decimal(5,1)) + '%' AS 'Percentage' •Error converting data type varchar to numeric. •CAST(ROUND(([Future]*100.0/Total), 2) as decimal(5,1)) AS 'Percentage' •This gives me the desired result (0.0), but without the % symbol. •str(ROUND(([Future]*100.0/Total), 2)) + '%' AS ‘Percentage’ •This works but without the decimals (0%) STR(CAST(ROUND(([Future]*100.0/Total), 2) as decimal(5,2))) + '%' - This works but again without the decimals (0%) Not sure what am I doing wrong.
decimal
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

nidheesh.r.pillai avatar image
nidheesh.r.pillai answered
Try this please CAST((CAST(ROUND(([Future]*100.0/Total), 2) as decimal(5,1))) AS VARCHAR)+'%' AS [Percentage]
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sqlcraz avatar image sqlcraz commented ·
Thanks Nidheesh! It worked like a charm.
1 Like 1 ·
nidheesh.r.pillai avatar image nidheesh.r.pillai commented ·
Glad to know! Happy to help :)
1 Like 1 ·
ThomasRushton avatar image
ThomasRushton answered
What version of SQL Server? If you're on 2012 or above, there's the [`FORMAT`][1] function. SELECT FORMAT(1.0/2.0,'p') result: 50.00 % [1]: https://msdn.microsoft.com/en-GB/library/hh213505.aspx
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sqlcraz avatar image sqlcraz commented ·
Thanks Thomas. It is 2012 but I'm getting the message "'FORMAT' is not a recognized built-in function name."
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.