- Home
- Anonymous
- Sign in
- Create
- Ask a question
- Spaces
- Site Issues (NOT FOR DATABASE QUESTIONS)
- Explore
- Topics
- Questions
- Users
- Badges

Any help is appreciated. I would like the values to show 0.0%. I tried all the following methods: ,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%) Not sure what am I doing wrong.

sql-server-2012convertdecimalcast-convert
Comment

You need to do the math, then convert to string, and then add the percentage symbol. Also, I'm confused why you are rounding to 2 decimal places if you want to only show 1. Also, by default, the engine will round when converting to a decimal type with a smaller scale. That said, I'll give an example using ROUND but only converting to string. Try this: CONVERT(varchar(40), CONVERT(decimal(38, 2), Future*100.0/Total)) + '%'

I used varchar(10) only because you had decimal(5,1), so I assume 10 characters was more than enough. Try varchar(20) to see if that works. I'll update my answer.

Okay. I can solve that for you by using CONVERT instead of ROUND. Just set the precision and scale large enough to avoid any overflow.

**21** People are following this question.

Copyright 2022 Redgate Software.
Privacy Policy