1

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

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

它应该分为

col2          col3         col4
Data          7-8          5

请帮我解决这个问题。

4

4 回答 4

2

测试数据:-

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
于 2013-10-30T17:39:52.197 回答
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
于 2013-10-30T16:53:13.860 回答
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
于 2013-10-30T17:01:51.347 回答
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
于 2013-10-30T18:46:38.357 回答