Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Server 2000 - Arithmetic results

Status
Not open for further replies.

philsky

Programmer
Sep 21, 2001
8
US
I have seen some strange results of arithmetic functions. The simplest example is:

select 182.0/100 returns 1.820000

Another result I've seen is where 1.5 comes out as 1.4999999

What I really want to do is return all numbers with 3 significant digits.

Any suggestions?

Phil
 

Convert the results to a decimal data type.

select cast(182.0/100 As decimal(7,3))

SQL is using the float data type by default. Float data type provides approximate rather than exact results. Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Thanks for your response, but my question remains. I want the results of 182/100 to appear as simply 1.82

If I select exactly that - "182/100" - then it assumes integer division and returns 1.

Any other ideas?

Phil
 

My answer remains the same. Use Cast (or Convert if you prefer) to convert the result to Decimal with the precision you desire. Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Phil,
Use a decimal argument for one or both of the arguments, then use CAST or CONVERT to set the result to the precision you need. If you make one of the arguments decimal, it forces the whole expression to decimal.
For me, in Query analyser 182/100 = 1 but 182/100.0 was 1.820000.
-hal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top