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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Division 1

Status
Not open for further replies.

IAMINFO

MIS
Feb 21, 2002
62
US
Hello everyone,
When I run the select statement below, sql returns 91.00
When I use a calculator, the return is 91.389971
I would like the return to be 91.38, what am I doing wrong?

select CAST(858426/9393 AS DECIMAL(10,2))

Thank you for taking time.
 
Integer math is occurring before the cast operation.

Try this:

Code:
select CAST(858426/9393[!].0[/!] AS DECIMAL(10,2))

Without the .0, SQL interprets the values as integer, so integer math happens. By adding .0, sql interprets this as a decimal number, and you get the correct results.


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank you gmmastros that worked, question

How would I do this with 2 different column names with integer data types

select CAST(858426/9393.0 AS DECIMAL(10,2))



select CAST( ColA/ColB AS DECIMAL(10,2))



 
Select Cast([blue]Cast(ColA As Decimal(10,2))[/blue] / [green]Cast(ColB As Decimal(10,2))[/green] As Decimal(10,2))

Or.... the 1.0 trick...

Select CAST( 1.0 * ColA/ColB AS DECIMAL(10,2))



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top