CREATE FUNCTION [dbo].[GetNameOnCode]
(
@StrCodes NVARCHAR(MAX)='Code'
)
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(Name,'') FROM TableName 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