拆分管道分隔为新列

时间:2013-10-30 16:43:36

标签: sql sql-server sql-server-2008 tsql

我必须将一列(管道分隔)拆分为新列。

例如:第1列:Data|7-8|5

应该分成

col2          col3         col4
Data          7-8          5

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

玩这个游戏。它有点冗长,但说明了操作的每一步。我鼓励您提出任何可能的后续问题!

DECLARE @t table (
   piped varchar(50)
)

INSERT INTO @t (piped)
  VALUES ('pipe|delimited|values')
       , ('a|b|c');

; WITH x AS (
  SELECT piped
       , CharIndex('|', piped) As first_pipe
  FROM   @t
)
, y AS (
  SELECT piped
       , first_pipe
       , CharIndex('|', piped, first_pipe + 1) As second_pipe
       , SubString(piped, 0, first_pipe) As first_element
  FROM   x
)
, z AS (
  SELECT piped
       , first_pipe
       , second_pipe
       , first_element
       , SubString(piped, first_pipe  + 1, second_pipe - first_pipe - 1) As second_element
       , SubString(piped, second_pipe + 1, Len(piped) - second_pipe) As third_element
  FROM   y
)
SELECT *
FROM   z

答案 1 :(得分:1)

测试数据: -

create table #test (
    col1 varchar(50)
)
go
insert into #test values
    ('Data|7-8|5'),
    ('Data|asdsad|sad'),
    ('fish|c cx cx xc cc xc cx |ededededeed'),
    ('Data|iueroiheqroqer|ewoijewijewd5'),
    ('tune||5'),
    ('Data||')
go

功能(需要针对格式错误的输入进行更多防御性编程): -

create function dbo.GetDomain(
    @source varchar(1024),
    @delimiter varchar(10),
    @domain int
) returns varchar(1024) as begin
    declare @returnValue varchar(1024)
    declare @workingOn int
    declare @length int
    set @workingOn=0
    while @workingOn<@domain begin
        set @source=substring(@source,charindex(@delimiter,@source)+1,1024)
        set @workingOn+=1
    end
    set @length=charindex(@delimiter,@source)
    set @returnValue=substring(@source,1,case when @length=0 then 1024 else @length-1 end)
    return @returnValue
end
go

用途: -

select t.col1, 
    dbo.GetDomain(t.col1,'|',0) as col2, 
    dbo.GetDomain(t.col1,'|',1) as col3, 
    dbo.GetDomain(t.col1,'|',2) as col4
from #test t
go

生产: -

col1                                     col2  col3                 col4
Data|7-8|5                               Data  7-8                  5
Data|asdsad|sad                          Data  asdsad               sad
fish|c cx cx xc cc xc cx |ededededeed    fish  c cx cx xc cc xc cx  ededededeed
Data|iueroiheqroqer|ewoijewijewd5        Data  iueroiheqroqer       ewoijewijewd5
tune||5                                  tune                       5
Data||                                   Data

答案 2 :(得分:0)

可能有数百种比这更好的方法,但

declare @string as varchar(100)
set @string = 'Data|7-8|5'
select left(@string,CHARINDEX('|',@string,0) -1) as one, 
left(substring(@string,CHARINDEX('|',@string,0)+1,100),CHARINDEX('|',substring(@string,CHARINDEX('|',@string,0)+1,100),0) -1) as two,
reverse(left(reverse(@string),CHARINDEX('|',reverse(@string),0) -1)) as three

答案 3 :(得分:0)

我最喜欢的解决此类问题的方法之一是使用XML数据类型。我不太了解它的表现,但它确实起到了作用。

DECLARE @line VARCHAR(MAX);
DECLARE @x xml;

/* example data */
SET @line = 'Data|7-8|5';


/* replace the delimeter with closing and opening tags,
   and wrap the line with opening and closing tags as well */
SET @x = Cast(
        '<field>'
        + replace(@line, '|', '</field><field>')
        + '</field>' AS XML);


/* query the nodes based on index */
SELECT
    @x.query('field[1]').value('.','VarChar(10)')   Col1
    ,@x.query('field[2]').value('.','VarChar(10)')  Col2
    ,@x.query('field[3]').value('.','Int')          Col3