Home > Database > Mysql Tutorial > How can I split a single column of multiple subscription numbers into individual columns in SQL?

How can I split a single column of multiple subscription numbers into individual columns in SQL?

DDD
Release: 2024-12-17 01:47:24
Original
143 people have browsed it

How can I split a single column of multiple subscription numbers into individual columns in SQL?

Splitting Subscription Numbers into Multiple Columns

In a situation where a single column contains multiple values, such as a subscription number, splitting these values into individual columns can be a useful data manipulation technique. Here's a step-by-step guide:

1. Cross Apply XML Fragmentation:

This approach utilizes XML fragmentation to convert the subscription number into an XML document, allowing individual values to be extracted as separate nodes.

Cross Apply (
    Select Pos1 = ltrim(rtrim(xDim.value('/x[1]','varchar(max)')))
          ,Pos2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
          ,Pos3 = ltrim(rtrim(xDim.value('/x[3]','varchar(max)')))
          ,Pos4 = ltrim(rtrim(xDim.value('/x[4]','varchar(max)')))
          ,Pos5 = ltrim(rtrim(xDim.value('/x[5]','varchar(max)')))
          ,Pos6 = ltrim(rtrim(xDim.value('/x[6]','varchar(max)')))
          ,Pos7 = ltrim(rtrim(xDim.value('/x[7]','varchar(max)')))
    From  (Select Cast('<x>' + replace((Select replace(replace(A.SomeCol,' ','-'),'-','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A 
) B
Copy after login

Here, xDim is the fragmentation element that converts the subscription number string into XML. The resulting nodes, numbered as x[1], x[2], and so on, are extracted into the target columns.

2. Creating Table on-the-fly:

Alternatively, you can create the target table dynamically during the query execution. This eliminates the need for an explicit table creation statement beforehand.

Select A.PUB_FORM_NUM
      ,B.*
 Into  MyNewPubTable
 From  @YourTable A
 Cross Apply (
                Select Pos1 = ltrim(rtrim(xDim.value('/x[1]','varchar(max)')))
                      ,Pos2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
                      ,Pos3 = ltrim(rtrim(xDim.value('/x[3]','varchar(max)')))
                      ,Pos4 = ltrim(rtrim(xDim.value('/x[4]','varchar(max)')))
                      ,Pos5 = ltrim(rtrim(xDim.value('/x[5]','varchar(max)')))
                      ,Pos6 = ltrim(rtrim(xDim.value('/x[6]','varchar(max)')))
                      ,Pos7 = ltrim(rtrim(xDim.value('/x[7]','varchar(max)')))
                From  (Select Cast('<x>' + replace((Select replace(replace(A.PUB_FORM_NUM,' ','-'),'-','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A 
             ) B
Copy after login

This approach creates the MyNewPubTable and inserts the split subscription number values directly into the targeted columns.

The above is the detailed content of How can I split a single column of multiple subscription numbers into individual columns in SQL?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template