Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Decimals truncated when storing decimal in a deicmal variable or colum


Decimals truncated when storing decimal in a deicmal variable or colum

Author
Message
charris1119
charris1119
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 73
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
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47143 Visits: 44346
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, MVP, M.Sc (Comp Sci)
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


charris1119
charris1119
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 73
Hi Gail,

Thanks so much for the reply.

If it weren't for stupid mistakes...

Thank you again.

Casey
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search