Monday, 3 July 2017

Get Name On Code in Comma Separate List Sql



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

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...