Solved

sql server - decimal vs real datatypes

Posted on 2010-09-14
16
730 Views
Last Modified: 2012-05-10
How many decimal places for the decimal vs real datatypes? what would happen if i updated a real datatype to decimal.

some of the values are like this for example:

0.879999995231628
0
Comment
Question by:troyvw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 3
  • +1
16 Comments
 
LVL 11

Accepted Solution

by:
Larissa T earned 250 total points
ID: 33676805
in decimal definition decimal(N,M)
m will specify number of digit after decimal point, N total number of positions, including decimal point

depending on you decimal definition this it can be truncated

declare @dec decimal(19,2),      @real float
set @real=0.879999995231628
set @dec=@real
select @dec



0
 

Author Comment

by:troyvw
ID: 33676843
if i change the real to decimal(38, 0) I would have 38 total characeters correct?
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 33676855
If you convert real to decimal, SQL will round as necessary to meet the new definition.

You only need real for **extremely** large or **extremely** small numbers or for lengthy involved computations.  Otherwise use decimal, since it's much more exact and easier to use.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 11

Expert Comment

by:Larissa T
ID: 33676886
decimal(38,0)  means no decimal point. You real number will be converted to 1
declare @dec decimal(38,0),      @real real

set @real=0.879999995231628
set @dec=@real
select @dec
0
 

Author Comment

by:troyvw
ID: 33676887
It would round to the nearest what?
0
 

Author Comment

by:troyvw
ID: 33676903
What decimal should i used then?
0
 

Author Comment

by:troyvw
ID: 33676910
I want to have some decimal places
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33676931
A real datatype like any other float is very imprecise.  If your data requires precise values, changing to the decimal is the right way to go.
http://msdn.microsoft.com/en-us/library/ms173773.aspx

Now it seems your question is the right Decimal value to use.  The question there would come in what is the number of decimal places you want to go out to and do you expect to get a equally long whole number.

0.879999995231628

If the above represents your target data and have no whole number values, you could use:

decimal(15, 15)

Alternatively you could go decimal(30, 15) or decimal(38, 19) and split down the middle to accept :
879999995231628.0
or
0.879999995231628
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33676952
Arg. I guess I should have said "ditto" since Scott said the same thing here http:#a33676855

To exemplify Scott's point:
declare @real real
set @real = 0.879999995231628;
select convert(decimal(38, 10), @real);
-- results in 0.8799999952 << rounded to 10th decimal place
0
 

Author Comment

by:troyvw
ID: 33676963
so that i undertand

decimal(15, 15)


would allow 15 to the left and 15 to the right of the decimal point?

decimal(13, 17)

would allow 13  to the left and 17 to the right of the decimal point?
0
 

Author Comment

by:troyvw
ID: 33676989
OK i undestand now. thank you
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33677008
decimal(N,M)
M will specify number of digit after decimal point, N total number of digits

decimal(13, 17) is invalid
The scale must be less than or equal to the precision.

decimal(15, 15)
will have only 15 position AFTER decimal point


0
 

Author Comment

by:troyvw
ID: 33677018
OK thank you
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33677051
Yes, agree with previous Expert.  To allow 15 to left and right, you will need decimal(30, 15) as I said in my post.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 33679064
>> It would round to the nearest what? <<

As I stated, "SQL will round as necessary to meet the new definition".

So however many decimal places are in the new column data type, SQL will automatically round to that.


0
 

Author Closing Comment

by:troyvw
ID: 33691601
ty
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question