Wants to create a function of the average product standardcost for the same color, and using 'red' to test . But NULL returned.
If only run the SELECT,FROM,WHERE,GROUP BY, it will work file and return a value.
Here's my code :
CREATE FUNCTION avgv (@Color CHAR)
RETURNS CHAR
AS
BEGIN
DECLARE @AVG CHAR ;
SET @AVG = (SELECT P.Color AVG(P.StandardCost)
FROM AdventureWorksLT.SalesLT.Product P
WHERE P.Color = @Color
GROUP BY Color)
Return @AVG
END
GO
SELECT dbo.avgv('red')
Please let me know how to fix, thanks!
VARchar.CHARsame asVARCHARwithoud explicitly defined length means length=1 (one symbol).AVG.