Home > Database > Mysql Tutorial > How to Transform Comma-Separated Values in a SQL Column into Separate Rows?

How to Transform Comma-Separated Values in a SQL Column into Separate Rows?

Mary-Kate Olsen
Release: 2025-01-05 10:06:45
Original
1008 people have browsed it

How to Transform Comma-Separated Values in a SQL Column into Separate Rows?

Querying SQL to Divide Column Data into Rows

Problem:

You have a table with two columns, Code and Declaration. The Declaration column contains a comma-separated list of values. You need to convert this data into rows, with each row representing a separate declaration for the corresponding code.

Solution:

One approach to address this issue involves creating a custom SQL function:

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

You can then leverage this function in a query using an outer apply to connect to your existing table:

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

This query will produce the desired result:

| CODE |  DECLARATION |
-----------------------
|  123 |     a1-2 nos |
|  123 |  a2- 230 nos |
|  123 |    a3 - 5nos |
Copy after login

Alternatively, you can use a CTE (Common Table Expression) version, which operates similarly:

;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 Values in a SQL Column into Separate Rows?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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