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
 SQL Server Administration (2005)
 SQL Outputs 0 when I know it should output a value

Author  Topic 

Dasman
Yak Posting Veteran

79 Posts

Posted - 2011-06-24 : 13:08:26
Dear All,

I have this function:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER FUNCTION [dbo].[Function3] (@PatientID int, @RecordingID int)

RETURNS TABLE
AS
RETURN (SELECT(SELECT * FROM dbo.Function1 (@PID, @RID)) / (SELECT * FROM dbo.Function2 (@PID, @RID)) AS Function3)

Now when this function along with Functions 1 and 2 are ran in a stored procedure for a certain PID and RID I get the following results:

Function1 = 102
Function2 = 131
Function3 = 0

Should I CAST as something? Why doesn't it output a number?

Thanks,
Dasman


==========================
Pain is Weakness Leaving the Body.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-24 : 13:10:17
RETURN (SELECT 1.0 * (SELECT * FROM dbo.Function1 (@PID, @RID)) / (SELECT * FROM dbo.Function2 (@PID, @RID)) AS Function3)


102/131 truncated to an integer = 0

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Dasman
Yak Posting Veteran

79 Posts

Posted - 2011-06-24 : 13:16:14
AWESOME! that was soo quick! Thank you Nigel and it now yields a value of 0.778625954198
which is mathematically correct!

One followup - How can i chose for it round off after two decimal places? To make the answer: 0.78? or 0.779?

Dasman


==========================
Pain is Weakness Leaving the Body.
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-06-29 : 17:48:34
Lookup ROUND function in Books Online.

=======================================
I have never met a man so ignorant that I couldn't learn something from him. -Galileo Galilei, physicist and astronomer (1564-1642)
Go to Top of Page
   

- Advertisement -