June 22, 2009 at 5:34 pm
I need help with a basic select that is not returning the correct decimal results......
The query:
select a.storeid,
a.customer_count
,b.customer_count
,cast(a.customer_count / b.customer_count as decimal (8,8))
from table1 a, table2 a
The reulsts:
6117439.00000000
My issue is that if you take a calculator and do 1/17439 = .00005734
Any idea why it only returns 0's in the select statement??
June 23, 2009 at 7:15 am
It's because when you do math in the select it implicitly convert the result to the lowest datatype. In this example 1 and 17439 are both integers so the result will be an integer. 0.00005734 as in integer is 0. Try casting one (or both) of the values going in as a decimal.
something like this:
select cast(cast(1 as decimal(18,8))/17439 as decimal(8,8))
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply