Link to home
Start Free TrialLog in
Avatar of Natchiket
NatchiketFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Scalar function not behaving consistently in query

In the following scalar function,

a fractional value between 0.1 and 0.4 is set to 0.5

a fractional value of 0.6 and above is rounded up to the next integer

(     @val float ) RETURNS float AS BEGIN     -- Declare the return variable here     declare @frac float      --fractional part of the value     declare @newfrac float     Set @frac = @val - round(@val,0,1)     set @newfrac = 0     If  @frac >= 0.1 and @frac <=0.4         set @newfrac = 0.5     else if @frac >= 0.6         set @newfrac = 1     else if @frac > 0.4 and @frac < 0.6         set @newfrac = 0.5     return     round(@val,0,1) + @newfrac END

Open in new window

this works fine in SMSS e.g.

select dbo.BHERound(1.6) , gives the answer 2, as expected

however the following query

select *,dbo.BHERound(RawBHE) as BHE from tblTESTRawBHE where PosRef='FCILLC002220'

Open in new window


User generated imagei.e 1.5 which is wrong, RawBHE is a FLOAT data type.

Avatar of Natchiket
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why didn't you just use CEILING ?
DECLARE @n float = 1.6
SELECT CEILING((@n-0.099999999)*2)/2.0

Open in new window