在多行中显示一列

时间:2012-03-14 12:30:16

标签: sql sql-server sql-server-2005

我一直在寻找一种方法来显示多行中的一列,一个单元格。它的内容用逗号分隔。

例如,取代:

ProjectID标签     ---- ---     1200 label1     1200 label2     1200 label3

我希望我的查询结果如下:

ProjectID                    Label
————                   ——–
1200                          label1, label2, label3

提前致谢

4 个答案:

答案 0 :(得分:3)

有不同的方法可以做到这一点。一种选择是创建一个表值函数,在不同的记录上“拆分”多值单元格。以下是拆分功能的示例:

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

创建后,您可以执行以下操作以获得结果:

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

答案 1 :(得分:2)

在这里,我制作了表值函数,它将字符串拆分并将结果作为所需的

返回
--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 :(得分:0)

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;

答案 3 :(得分:-1)

使用SQL Server表函数和split函数返回表