Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Problems with SQL Function

Author  Topic 

gavinjb
Starting Member

34 Posts

Posted - 2007-11-27 : 06:55:29
Hi,

I have writen a simple Function that calls various other functions and should return a value of 1.75 or less, only problem is that when the value is < 1 it return 0 for example one set of parameters I passed if I did manually I would get a result of 0.54 but the function returns 0 can anyone please help.


ALTER FUNCTION [dbo].[SLN]
(
@GLH as smallint, @Year AS smallint, @A27 AS smalldatetime, @A28 AS smalldatetime
)
RETURNS Float
AS --calculates if aim is active on first census point
BEGIN
DECLARE @AGLH AS SmallInt, @SLNResult AS Float

-- Get ALGH
SELECT @AGLH = (SELECT dbo.AGLH (@GLH, @Year, @A27, @A28))

SET @SLNResult = (@AGLH / 450)

IF @SLNResult > 1.75
BEGIN
SET @SLNResult = 1.75
END

RETURN @SLNResult
END


If I called this function with

select dbo.SLN (486,2008,'04-Sep-2007', '03-Jul-2009')

I would expect to get 0.54 but I am getting 0. for info the dbo.AGLH function returns a value of 243.

Thanks,



Gavin,

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-11-27 : 06:57:27
quote:
Originally posted by gavinjb

Hi,

I have writen a simple Function that calls various other functions and should return a value of 1.75 or less, only problem is that when the value is < 1 it return 0 for example one set of parameters I passed if I did manually I would get a result of 0.54 but the function returns 0 can anyone please help.


ALTER FUNCTION [dbo].[SLN]
(
@GLH as smallint, @Year AS smallint, @A27 AS smalldatetime, @A28 AS smalldatetime
)
RETURNS Float
AS --calculates if aim is active on first census point
BEGIN
DECLARE @AGLH AS SmallInt, @SLNResult AS Float

-- Get ALGH
SELECT @AGLH = (SELECT dbo.AGLH (@GLH, @Year, @A27, @A28))

SET @SLNResult = (@AGLH / 450.0)

IF @SLNResult > 1.75
BEGIN
SET @SLNResult = 1.75
END

RETURN @SLNResult
END


If I called this function with

select dbo.SLN (486,2008,'04-Sep-2007', '03-Jul-2009')

I would expect to get 0.54 but I am getting 0. for info the dbo.AGLH function returns a value of 243.

Thanks,



Gavin,



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2007-11-27 : 07:00:09
If you are looking for results with decimal places then the variables used in the calculation shpuld cater for this.
DECLARE @AGLH AS SmallInt -- should be something like DECLARE @AGLH AS DECIMAL(8, 4)

and SET @SLNResult = (@AGLH / 450) -- should be something like SET @SLNResult = (@AGLH / 450.0000000)



Duane.
Go to Top of Page

gavinjb
Starting Member

34 Posts

Posted - 2007-11-27 : 07:01:14
Thanks for the help, I just realized the same that I was returning an Int for AGLH.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-11-27 : 07:04:28
Actually you don't need to @AGLH to float/decimal, since SQL Server automatically cast to float/decimal since one of the operand is float/decimal.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -