I have a table that look like that
projectID , year , jan , feb , mar , apr , may ....
each one reprsent a numeric coulm
and I want to get the value at a specific colums that determine by a value that sended to the function.
I Have this Function
CREATE FUNCTION [dbo].[func_]
( @BRANCH_ID NUMERIC(6,0) = 0,
@JOB_TYPE_ID NUMERIC(6,0) = 0,
@PROJECT_ID NUMERIC(6,0) = 0,
@MONTH_NAME varchar(10) = 0,
@YEAR_NUMBER NUMERIC(6,0) = 0)
RETURNS NUMERIC(6,1)
AS
BEGIN
DECLARE @FORCAST NUMERIC(6,1)
SELECT @FORCAST = @MONTH_NAME
FROM TABLE
WHERE Projectid = @PROJECT_ID
AND Year = @YEAR_NUMBER
IF @FORCAST IS NULL
RETURN(0)
RETURN(@FORCAST)
END
For example if I execute the function today and send the year and month of today it will return the value in “mar” column.
You could use
UNPIVOTif it’s available in your version of SQL Server (it’s available starting from SQL Server 2005):Otherwise it could be something like this: