Saturday, July 2, 2011

SQL SERVER - UDF - Get Name as per specific format

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

No comments:

Post a Comment

How to add the .NET MAUI App Accelerator in Visual Studio

 To add the .NET MAUI App Accelerator in Visual Studio, follow these steps: Prerequisites: Ensure you have Visual Studio 2022 (17.3 or lat...