Show one column in multiple rows

I was looking for a way to show one column in multiple rows, one cell at a time. The contents of this section are separated by commas.

For example, instead of:

ProjectID tag ------ 1200 label1 1200 label2 1200 label3

I want the result of my query to look like this:

ProjectID                    Label
โ€”โ€”โ€”โ€”                   โ€”โ€”โ€“
1200                          label1, label2, label3

      

early

+3


source to share


4 answers


There are different ways to do this. One option is to create a table-valued function that "splits" your multivalued cell into different records. Below is an example of the split function:

ALTER FUNCTION [dbo].[Split](@RowData VARCHAR(MAX), @SplitOn VARCHAR(5))  
RETURNS @RtnValue TABLE 
(
    Id int identity(1,1),
    Data VARCHAR(MAX)
) 
AS  
BEGIN 
    Declare @Cnt int
    Set @Cnt = 1

    While (Charindex(@SplitOn,@RowData)>0)
    Begin
        Insert Into @RtnValue (data)
        Select 
            Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

        Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
        Set @Cnt = @Cnt + 1
    End

    Insert Into @RtnValue (data)
    Select Data = ltrim(rtrim(@RowData))

    Return
END

      



Once created, you can do the following to get the results:

SELECT *
FROM YourTable A
CROSS APPLY dbo.Split(Label,', ') B

      

+3


source


Here I made a Table Valued Function that splits the string and returns the result as desired



--Create the function
    CREATE FUNCTION dbo.Split(@ProjectId nvarchar(50),@String varchar(8000), @Delimiter char(1))       --Pass projectID,label and delimiter and returns table 
    returns @temptable TABLE (id nvarchar(50),items varchar(8000))       
    as       
    begin       
        declare @idx int       
        declare @slice varchar(8000)       

        select @idx = 1       
            if len(@String)<1 or @String is null  return       

        while @idx!= 0       
        begin       
            set @idx = charindex(@Delimiter,@String)       
            if @idx!=0       
                set @slice = left(@String,@idx - 1)       
            else       
                set @slice = @String       

            if(len(@slice)>0)  
                insert into @temptable(id,Items) values(@ProjectId,@slice)       

            set @String = right(@String,len(@String) - @idx)       
            if len(@String) = 0 break       
        end   
    return       
    end  
--Calling the function
select * from dbo.split('1200',' label1, label2, label3',',')  --calling teh function

      

+2


source


create table #comma_seprate
(ProductID int,
Lable varchar(max))

declare @index int, @id int;
declare @lable varchar(max);
declare cur_comma cursor
for select ProductID, Lable from comma_seprate
open cur_comma
fetch next from cur_comma into @id, @lable
while (@@fetch_status=0)
begin
    set @index=charindex(',',@lable);
    while(@index>0)
    begin
        insert into #comma_seprate values (@id,rtrim(ltrim(left(@lable,@index-1))));
        set @lable=substring(@lable,@index+1,len(@lable));
        set @index=charindex(',',@lable);
    end
    insert into #comma_seprate values (@id, rtrim(ltrim(@lable))); 
    fetch next from cur_comma into @id,@lable;
end
close cur_comma;
deallocate cur_comma;
select * from #comma_seprate;
truncate table #comma_seprate;

      

0


source


Use SQL Server table function with split function that returns a table

-1


source







All Articles