Tutorial :SQL Server Cast and Rounding



Question:

I have in my select clause:

AVG (cast(scale as decimal(5,2)))  

I keep getting a number like: 0.6523412897, nothing I do seems to get me to my desired: 0.65. Basically I want to have a scale of 2 (two decimal places). Thanks


Solution:1

Try

ROUND(AVG(scale), 2)  


Solution:2

Cast the average, don't average the cast:

cast(AVG(scale) as decimal(5,2))  

Update

Using ROUND() changes the value not the type. Eg. select round(0.12345,2) returns 0.12000 as it should becuase it rounds 0.12345 to 0.12 but keeps the original type with 5 decimals after the point, hence 0.12000. To change the type one must use cast, as in my post.


Solution:3

It depends upon the database you are using, but ROUND is the answer for SQL Server:

ROUND


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »