Sql 将分隔为新列的管道拆分
我必须将一列管道分隔成新的列 例:第1栏:数据| 7-8 | 5 它应该分成两部分Sql 将分隔为新列的管道拆分,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我必须将一列管道分隔成新的列 例:第1栏:数据| 7-8 | 5 它应该分成两部分 col2 col3 col4 Data 7-8 5 请帮我解决这个问题。玩一玩。这有点冗长,但说明了操作的每个步骤。我鼓励您提出任何后续问题 DECLARE @t table ( piped varchar(50) ) INSERT INTO @t (piped) VALUES ('pipe|delimited|values')
col2 col3 col4
Data 7-8 5
请帮我解决这个问题。玩一玩。这有点冗长,但说明了操作的每个步骤。我鼓励您提出任何后续问题
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
可能有数百种比这更好的方法,但是
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
测试数据:-
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
产生:-
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
我最喜欢的解决这类问题的方法之一是使用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
总是有固定数量的管道吗?是的,只有两条管道。。必须分成三列询问代码必须表明对所解决问题的最低理解。包括尝试的解决方案,以及它们不起作用的原因。选择col1,LEFTcol1,isnullifcharindex'|',col1-1,-1,LENcol1作为col2,LEFTcol2,ISNULLNULLIFCHARINDEX'|',col1-1,-1,LENcol1-1作为col3,RIGHTcol1,LENcol isnullifcharindex'|',col1-1,-2,LENcol1作为col4从mytableI上面的查询中尝试过。我无法正确获取col2,但无法将第二个管道拆分为col3和col4。
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