Monday, July 6, 2009

Scalar Valued Functions in SQL Server - Sample To Retrieve Name

This Function takes ID as the input Parameter and fetches down the name and returns it.

CREATE function [dbo].[fnName](@ID int)
returns varchar(200)
as
begin
declare @Name varchar(200)
set @Name=''

SELECT @Name= LTRIM(RTRIM(REPLACE(ISNULL(E.FirstName,'')+' '+ISNULL(E.MiddleName,'')+' '+ISNULL(E.LastName,''),' ',' ')))
FROM dbo.USERInfo E
WHERE E.USERID=@ID

set @Name=ISNULL(@Name,'')

return @Name
end

No comments: