Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Function @return

Status
Not open for further replies.

747576

Programmer
Jun 18, 2002
97
GB
Hi there,
Below is a function which returns a value or name. The @return is declared as varchar, how do I change the sql to set the @return to FLOAT if its value. If the value is returned it has to be numeric.
Thanks



CREATE FUNCTION NEW_VALUE (@id VARCHAR(30)
, @field CHAR(1))
RETURNS VARCHAR(30)
AS

BEGIN
DECLARE @value VARCHAR(30)
DECLARE @vname VARCHAR(30)
DECLARE @return VARCHAR(30)

SELECT @vname = T1.VNAME, @value = CONVERT(VARCHAR,T1.VALUE)
FROM
(SELECT T.ID
,T.NAME VFNAME
,CASE WHEN T.VALUE < 0 THEN T.VALUE * -0.5 ELSE T.VALUE END VALUE
FROM TABLE T
) T1
IF @field = 'N'
SET @return = @value
ELSE
SET @return = @vname

RETURN @return
END
GO
/*******************************************************************************
Usage: SELECT dbo.NEW_VALUE('123','N')
SELECT dbo.NEW_VALUE('123','Y')
********************************************************************************/
 
you will probably have better luck getting an answer in the Microsoft SQL Server forum (or is this perhaps Sybase?)

this is the ANSI SQL forum

FYI: the subquery T1 does not have a column called VNAME

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top