Monday, 6 February 2017

Comma Separate string return comma separated value in sql server from table using function

// Create function

create FUNCTION [dbo].[CommanTable_CommaSaparated_FromSaparated](@StrClm VARCHAR(MAX),@Flag int)
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @ListStr VARCHAR(MAX),@Ids varchar(50) = ''
set @Ids=','+@StrClm+','
SELECT @ListStr = COALESCE(@ListStr+',' ,'') + Value FROM tblMstCommon where Charindex(','+cast(ID as varchar(8000))+',', @Ids) > 0 and Flag=@Flag
RETURN @ListStr

END



// Call function 

select dbo.CommanTable_CommaSaparated_FromSaparated('1,2,3,',525)as Result


// REsult




No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...