Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Decimals truncated when storing decimal in a deicmal variable or colum Expand / Collapse
Author
Message
Posted Tuesday, August 27, 2013 10:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, January 31, 2014 8:49 PM
Points: 7, Visits: 52
Greetings All,

I've got the following weird issue. I am performing division which is resulting in a decimal number. If I then store that number in a decimal variable or insert it into a decimal column in a table, the decimals are truncated. I've tried this in both SQL2005 & SQL2012 with the same results. Can anyone explain why this is occurring? Example code below.

Thanks in advance,
Casey

declare @a decimal -- dividend
declare @b decimal -- divisor
declare @c decimal -- result
declare @tbl table (col1 decimal)

set @a = 247
set @b = 192

-- Result 1.
select @a / @b

-- Result 2.
select @c = @a / @b
select cast(@c as decimal)

-- Result 3.
insert @tbl (col1)
values (@a/@b)
select * from @tbl
Post #1488860
Posted Tuesday, August 27, 2013 10:45 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:49 AM
Points: 39,952, Visits: 36,307
You're not declaring the precision or scale of the decimal. The default is 18,0 meaning 18 digits on the left of the decimal place and 0 on the right (so whole numbers only). If that's not what you want, you need to explicitly specify the precision and scale that you want the variable to have.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1488862
Posted Wednesday, August 28, 2013 7:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, January 31, 2014 8:49 PM
Points: 7, Visits: 52
Hi Gail,

Thanks so much for the reply.

If it weren't for stupid mistakes...

Thank you again.

Casey
Post #1489197
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse