CREATE FUNCTION [dbo].[GetNameOnCode]
(
@StrCodes NVARCHAR(MAX)='C-05465,C-00535,C-00538,C-03865,C-06377'
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @Separator CHAR(1)
DECLARE @SeparatorPosition INT
DECLARE @strCode NVARCHAR(50)
SET @Separator = ','
DECLARE @NewStr NVARCHAR(MAX)
DECLARE @NewName NVARCHAR(MAX)=''
SET @StrCodes=@StrCodes+','
--SELECT @StrCodes
WHILE PATINDEX('%' + @Separator + '%', @StrCodes) <> 0
BEGIN
SET @SeparatorPosition = PATINDEX('%' + @Separator + '%', @StrCodes)
SET @strCode = LEFT(@StrCodes, @SeparatorPosition - 1)
--SELECT @strCode
SET @NewStr=ISNULL((SELECT TOP 1 ISNULL(CustName,'') FROM CustomerMaster WHERE LTRIM(RTRIM(No))=LTRIM(RTRIM(@strCode))),'')
SET @NewName=@NewName+@NewStr+','
SET @StrCodes = STUFF(@StrCodes, 1, @SeparatorPosition, '')
END
RETURN @NewName
END
No comments:
Post a Comment