January 9, 2004 at 4:20 am
On dividing two integer columns in a sql table how to get the result as a number with 2 decimal places.
example
select Elevation/Height As TValue from Camps
(where Elevation and Height are integer columns and I want the resultant TValue as a number with 2 decimal places)
Thanks a lot.
January 9, 2004 at 4:34 am
select Elevation/Height*1.00 As TValue from Camps should work, but why as this will always be .00?
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
January 9, 2004 at 5:39 am
Thanks but no I do not want it to be always .00?
example if Elevation/Height (40/3)=13.33 the result should be 13.33 and not 13.00
January 9, 2004 at 6:14 am
Would select cast(Elevation as decimal(8,2))/Height As TValue from Camps help?
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
January 9, 2004 at 6:26 am
No luck it still shows as 13.3333333333333 and not 13.33
January 9, 2004 at 6:26 am
If you're using the result as a number, cast it to decimal. If you're just displaying the result, you can use STR().
SELECT CAST(Elevation*1./Height AS dec(9,2)), STR(Elevation*1./Height,10,2)
Multiplying by 1. is the lazy programmer's way to force an implicit conversion to decimal(n,0), which works in this case as you only want a scale of 2 and the minimum scale is 6 for the result of a division where any operand is decimal.
--Jonathan
January 9, 2004 at 6:29 am
So, you seem to need this for presentational purposes?
Well, use Jonathan's suggestion or, even better IMHO, do this in your application layer.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
January 9, 2004 at 6:33 am
Ok got it
Thanks all
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply