Home > Database > Mysql Tutorial > How to Transform Comma-Separated Column Data into Distinct Rows in SQL?

How to Transform Comma-Separated Column Data into Distinct Rows in SQL?

Patricia Arquette
Release: 2025-01-05 10:31:49
Original
666 people have browsed it

How to Transform Comma-Separated Column Data into Distinct Rows in SQL?

Extracting Row Data from Split Column Data in SQL

To split column data into distinct rows, you can leverage a custom function and apply it to your existing table using an outer join. This enables you to transform data like this:

Code  Declaration
123   a1-2 nos, a2- 230 nos, a3 - 5nos
Copy after login

Into the desired format:

Code  Declaration 
123   a1 - 2nos 
123   a2 - 230nos 
123   a3 - 5nos
Copy after login

Using a Split Function

Create a split function called [dbo].[Split] to separate the data using a ',' delimiter:

create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
returns @temptable TABLE (items varchar(MAX))       
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(Items) values(@slice)       

        set @String = right(@String,len(@String) - @idx)       
        if len(@String) = 0 break       
    end   
return 
end;
Copy after login

Applying the Split Function

Use the split function in a query to join the new table to the original:

select t1.code, s.items declaration
from yourtable t1
outer apply dbo.split(t1.declaration, ',') s
Copy after login

This will produce the desired output.

Using a CTE

Alternatively, you can implement a CTE (Common Table Expression) version:

;with cte (code, DeclarationItem, Declaration) as
(
  select Code,
    cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
         stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
  from yourtable
  union all
  select code,
    cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
    stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
  from cte
  where Declaration > ''
) 
select code, DeclarationItem
from cte
Copy after login

The above is the detailed content of How to Transform Comma-Separated Column Data into Distinct Rows in SQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template