Microsoft Sql Rounding Off To Whole Number, Need To 1 Decimal Place
Possible Duplicate: Truncate (not round) decimal places in SQL Server Can't figure this out. I need 1 decimal place returned while SQL is rounding to whole numbers. I read that
Solution 1:
SET@intinterval=cast(10* (@b/1000.0) * (86400.0/@a) asint) /10.0
or
SET@intinterval=cast(@b*864.0/@aasint) /10.0
Solution 2:
What about Round((@b / 1000.0) * (86400.0 / @a), 1, 1)
, where last 1 saying to truncate instead of round.
Solution 3:
try this with no special functions...
if a = 5 then output = 34.5 (34.56) a = 7 output = 24.6 (24.69) a = 11 output = 15.7 (15.71)
createtable #blah(output decimal(9,1))
DECLARE@a money
DECLARE@b money
DECLARE@intintervaldecimal(9,2)
declare@roundeddecimal(9,1)
declare@diffdecimal(9,2)
declare@finalvaluedecimal(9,1)
SET@a=5SET@b=2SET@intinterval= (@b/1000.0) * (86400.0/@a)
set@rounded=@intinterval-- gets the rounded valueset@diff=@intinterval-@rounded-- gets the difference whether to round off or not
if @diff>=0-- if differnce is >= 0 then get the rounded value .. eg. 34.31 = 34.3set@finalvalue=@roundedelse-- else subtract 0.1 and get the rounded value e.g. 34.56 - 0.1 = 34.46 -> rounded value of 34.5set@finalvalue=@intinterval-0.1INSERTINTO #blah (output) VALUES (@finalvalue )
SELECT*from #blah
droptable #blah
Post a Comment for "Microsoft Sql Rounding Off To Whole Number, Need To 1 Decimal Place"