Solved

Rounding problem in SQL 2005

Posted on 2008-06-18
4
188 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
Comment
Question by:jealanish
4 Comments
 
LVL 13

Expert Comment

by:rickchild
ID: 21814410
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

by:jealanish
ID: 21814561
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

by:Mark Wills
ID: 21815774
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

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 21816012
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

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Review MS SQL cluster diagram 9 81
Set the max value for a column 7 37
SQL Restore Script - Syntax Error 8 91
How to place a condition in a filter criteria in t-sql (#2)? 10 41
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now