Solved

Rounding problem in SQL 2005

Posted on 2008-06-18
176 Views
Last Modified: 2010-03-19
I have data for a particular row. the value as shown when i open the table is 0.02222499999999999998 and when i execute a query in the SQL management studio, the value returned is 0.022225. how can i make the query return the exact data in the table and not the rounded value.
0
Question by:jealanish
4 Comments

LVL 13

Expert Comment

I am sure someone will come in with a better solution, but see this:

http://www.codeproject.com/KB/database/Formatting_in_SQL.aspx
0

Author Comment

It does not really help me as the value that is retrieved is 0.022225 and so whatever operation you do is based on this. i see the value as 0.02222499999999999998 in the table.  lets say i have a table expt in which i have two columns one is name of type varchar2 and the other is floatvalue of type float. insert into expt values('jealani' , 0.02222499999999999998). now when i execute the query 'Select * from expt". i need to see "Jealani   0.02222499999999999998 " as the output. i need the exact value what i see in the table in the table view mode.
0

LVL 51

Expert Comment

Sounds like you are using a float. which is an approximation to the real value. Make sure your datatype is either REAL or FLOAT when retrieving, or if decimal, has sufficent scale to accommodate such as decimal(24,18). When using float, some of the views like tableview will attempt to round back. Better if possible to use a high scaled number rather than the numerical approximation of a float column...
0

LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 250 total points
just to show the "behaviour":
``````set nocount on

declare @i1 float

declare @i2 float

declare @f float

set @i1 = 1

set @i2 = 3

set @f = @i1/@i2

select @f f

select cast(@f as varchar(1000)) v

select cast(@f as decimal(20,18)) d

returns:

f

----------------------

0,333333333333333

v

----------------------------------------------

0.333333

d

---------------------------------------

0.333333333333333310
``````
0

Join & Write a Comment Already a member? Login.

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

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

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!