Following Sql function can be used to get Name as per specific format
--This function uses table employee of pubs database which comes with sql server
CREATE FUNCTION [dbo].fnEmployeeName(@ID varchar(15), @Format int = 1) RETURNS varchar(30) AS
/****** fnEmployeeName******/
-- Return Employee Name as per the required format
BEGIN
DECLARE @FIRSTNM varchar(15), @LASTNM varchar(15), @MIDDLENM varchar(15)
SELECT @FIRSTNM = fname, @LASTNM = lname, @MIDDLENM = minit FROM employee WHERE emp_id = @ID
DECLARE @Value varchar(103) IF @@ROWCOUNT > 0
begin
IF @Format=1 SET @Value = LTRIM(IsNull(@LASTNM,'') + IsNull(' ' + @FIRSTNM,'') + IsNull(' ' + @MIDDLENM,''))
Else
SET @Value = LTRIM(IsNull(@FIRSTNM,'') + IsNull(' ' + @LASTNM,'') + IsNull(' ' + @MIDDLENM,'')) END
RETURN @Value
END
--This function uses table employee of pubs database which comes with sql server
CREATE FUNCTION [dbo].fnEmployeeName(@ID varchar(15), @Format int = 1) RETURNS varchar(30) AS
/****** fnEmployeeName******/
-- Return Employee Name as per the required format
BEGIN
DECLARE @FIRSTNM varchar(15), @LASTNM varchar(15), @MIDDLENM varchar(15)
SELECT @FIRSTNM = fname, @LASTNM = lname, @MIDDLENM = minit FROM employee WHERE emp_id = @ID
DECLARE @Value varchar(103) IF @@ROWCOUNT > 0
begin
IF @Format=1 SET @Value = LTRIM(IsNull(@LASTNM,'') + IsNull(' ' + @FIRSTNM,'') + IsNull(' ' + @MIDDLENM,''))
Else
SET @Value = LTRIM(IsNull(@FIRSTNM,'') + IsNull(' ' + @LASTNM,'') + IsNull(' ' + @MIDDLENM,'')) END
RETURN @Value
END
No comments:
Post a Comment