Lill Lansey
Lill Lansey

Reputation: 4915

Why am I losing the two decimal points in this cast expression?

What am I missing here?

select cast ( ( cast (100 * 39  as decimal )  / 41) as decimal(5,2)) 

gives a result of 95.12

but

declare @percent decimal 
set  @percent = cast ( ( cast (100 * 39  as decimal )  / 41)  as decimal(5,2)) 
select @percent     

gives a result of 95

What happened to the two 2 decimal points and how do I get them back into my variable?

Upvotes: 1

Views: 1287

Answers (1)

anon
anon

Reputation:

DECIMAL on its own does not have any decimal places (it's actually DECIMAL(18,0)). You need to specify precision and scale, e.g.

DECLARE @percent DECIMAL(5,2);
SET @percent = 1.0 * (100 * 39) / 41;
SELECT @percent;

Upvotes: 7

Related Questions