;WITH t(s)AS(
SELECT N'1|2|张三|王二麻子|5|6|7|8|9|10'
)
,a AS(
SELECT t.s,n.number FROM t
OUTER APPLY (SELECT sv.number FROM master.dbo.spt_values AS sv
WHERE sv.type='P'
AND sv.number BETWEEN CHARINDEX('|',t.s) AND LEN(t.s)
AND (((sv.number-LEN(REPLACE(LEFT(t.s,sv.number),'|','')))%5=0 AND SUBSTRING(t.s,sv.number,1)='|')
OR sv.number=LEN(t.s))
)n
)
SELECT a1.*,SUBSTRING(a1.s,ISNULL(b.number,0)+1,a1.number-(ISNULL(b.number,1)-1)) FROM a AS a1
OUTER APPLY(SELECT TOP 1 a2.number FROM a AS a2 WHERE a2.number<a1.number ORDER BY a2.number desc) b
/*
s number
------------------------ ----------- ------------------------
1|2|张三|王二麻子|5|6|7|8|9|10 14 1|2|张三|王二麻子|5|
1|2|张三|王二麻子|5|6|7|8|9|10 24 6|7|8|9|10
SELECT t1.s,t2.* FROM (
SELECT *,CONVERT(XML,'<n>'+REPLACE(t.s,'|','</n><n>')+'</n>') AS x FROM t
) t1
CROSS APPLY (SELECT (ROW_NUMBER()OVER(PARTITION BY t1.s ORDER BY GETDATE())-1)/5 AS groupid, s.b.value('.','nvarchar(100)') AS c FROM t1.x.nodes('n')s(b)) t2
CREATE FUNCTION fn_SplitTab ( @Str VARCHAR(MAX) )
RETURNS TABLE
AS
RETURN
( SELECT STUFF(a.Col.query('c[position()>sql:column("b.SNum") and position()<=sql:column("b.ENum")]').value('.',
'varchar(50)'),
1, 1, '') AS Col
FROM ( SELECT CONVERT(XML, '<c>|' + REPLACE(@Str, '|',
'</c><c>|') + '</c>') AS Col
) AS a
CROSS JOIN ( SELECT b.number * 5 AS SNum ,
( b.number + 1 ) * 5 AS ENum
FROM master.dbo.spt_values AS b
WHERE b.type = 'P'
AND b.number < CEILING(( LEN(@Str)
- LEN(REPLACE(@Str,
'|', '')) )
/ 5.0)
) AS b
);
GO
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10')
/*
1|2|张三|王二麻子|5
6|7|8|9|10
*/
#8
SQL SERVER函数分拆
e.g.
CREATE FUNCTION fn_SplitTab ( @Str VARCHAR(MAX) )
RETURNS TABLE
AS
RETURN
( SELECT STUFF(a.Col.query('c[position()>sql:column("b.SNum") and position()<=sql:column("b.ENum")]').value('.',
'varchar(50)'),
1, 1, '') AS Col
FROM ( SELECT CONVERT(XML, '<c>|' + REPLACE(@Str, '|',
'</c><c>|') + '</c>') AS Col
) AS a
CROSS JOIN ( SELECT b.number * 5 AS SNum ,
( b.number + 1 ) * 5 AS ENum
FROM master.dbo.spt_values AS b
WHERE b.type = 'P'
AND b.number < CEILING(( LEN(@Str)
- LEN(REPLACE(@Str,
'|', '')) )
/ 5.0)
) AS b
);
GO
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10')
/*
1|2|张三|王二麻子|5
6|7|8|9|10
*/
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10'执行不了,函数里有问题
#9
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10'执行不了,函数里有问题
;WITH t(s)AS(
SELECT N'1|2|张三|王二麻子|5|6|7|8|9|10'
)
,a AS(
SELECT t.s,n.number FROM t
OUTER APPLY (SELECT sv.number FROM master.dbo.spt_values AS sv
WHERE sv.type='P'
AND sv.number BETWEEN CHARINDEX('|',t.s) AND LEN(t.s)
AND (((sv.number-LEN(REPLACE(LEFT(t.s,sv.number),'|','')))%5=0 AND SUBSTRING(t.s,sv.number,1)='|')
OR sv.number=LEN(t.s))
)n
)
SELECT a1.*,SUBSTRING(a1.s,ISNULL(b.number,0)+1,a1.number-(ISNULL(b.number,1)-1)) FROM a AS a1
OUTER APPLY(SELECT TOP 1 a2.number FROM a AS a2 WHERE a2.number<a1.number ORDER BY a2.number desc) b
/*
s number
------------------------ ----------- ------------------------
1|2|张三|王二麻子|5|6|7|8|9|10 14 1|2|张三|王二麻子|5|
1|2|张三|王二麻子|5|6|7|8|9|10 24 6|7|8|9|10
*/
#5
;WITH t(s)AS(
SELECT N'1|2|张三|王二麻子|5|6|7|8|9|10'
)
,a AS(
SELECT t.s,n.number FROM t
OUTER APPLY (SELECT sv.number FROM master.dbo.spt_values AS sv
WHERE sv.type='P'
AND sv.number BETWEEN CHARINDEX('|',t.s) AND LEN(t.s)
AND (((sv.number-LEN(REPLACE(LEFT(t.s,sv.number),'|','')))%5=0 AND SUBSTRING(t.s,sv.number,1)='|')
OR sv.number=LEN(t.s))
)n
)
SELECT a1.*,SUBSTRING(a1.s,ISNULL(b.number,0)+1,a1.number-(ISNULL(b.number,1)-1)) FROM a AS a1
OUTER APPLY(SELECT TOP 1 a2.number FROM a AS a2 WHERE a2.number<a1.number ORDER BY a2.number desc) b
/*
s number
------------------------ ----------- ------------------------
1|2|张三|王二麻子|5|6|7|8|9|10 14 1|2|张三|王二麻子|5|
1|2|张三|王二麻子|5|6|7|8|9|10 24 6|7|8|9|10
*/
看不懂,可能改成函数,不会用sqlserver,只会oracle,谢谢
#6
;WITH t(s)AS(
SELECT N'1|2|张三|王二麻子|5|6|7|8|9|10'
)
,a AS(
SELECT t.s,n.number FROM t
OUTER APPLY (SELECT sv.number FROM master.dbo.spt_values AS sv
WHERE sv.type='P'
AND sv.number BETWEEN CHARINDEX('|',t.s) AND LEN(t.s)
AND (((sv.number-LEN(REPLACE(LEFT(t.s,sv.number),'|','')))%5=0 AND SUBSTRING(t.s,sv.number,1)='|')
OR sv.number=LEN(t.s))
)n
)
SELECT a1.*,SUBSTRING(a1.s,ISNULL(b.number,0)+1,a1.number-(ISNULL(b.number,1)-1)) FROM a AS a1
OUTER APPLY(SELECT TOP 1 a2.number FROM a AS a2 WHERE a2.number<a1.number ORDER BY a2.number desc) b
/*
s number
------------------------ ----------- ------------------------
1|2|张三|王二麻子|5|6|7|8|9|10 14 1|2|张三|王二麻子|5|
1|2|张三|王二麻子|5|6|7|8|9|10 24 6|7|8|9|10
SELECT t1.s,t2.* FROM (
SELECT *,CONVERT(XML,'<n>'+REPLACE(t.s,'|','</n><n>')+'</n>') AS x FROM t
) t1
CROSS APPLY (SELECT (ROW_NUMBER()OVER(PARTITION BY t1.s ORDER BY GETDATE())-1)/5 AS groupid, s.b.value('.','nvarchar(100)') AS c FROM t1.x.nodes('n')s(b)) t2
CREATE FUNCTION fn_SplitTab ( @Str VARCHAR(MAX) )
RETURNS TABLE
AS
RETURN
( SELECT STUFF(a.Col.query('c[position()>sql:column("b.SNum") and position()<=sql:column("b.ENum")]').value('.',
'varchar(50)'),
1, 1, '') AS Col
FROM ( SELECT CONVERT(XML, '<c>|' + REPLACE(@Str, '|',
'</c><c>|') + '</c>') AS Col
) AS a
CROSS JOIN ( SELECT b.number * 5 AS SNum ,
( b.number + 1 ) * 5 AS ENum
FROM master.dbo.spt_values AS b
WHERE b.type = 'P'
AND b.number < CEILING(( LEN(@Str)
- LEN(REPLACE(@Str,
'|', '')) )
/ 5.0)
) AS b
);
GO
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10')
/*
1|2|张三|王二麻子|5
6|7|8|9|10
*/
#8
SQL SERVER函数分拆
e.g.
CREATE FUNCTION fn_SplitTab ( @Str VARCHAR(MAX) )
RETURNS TABLE
AS
RETURN
( SELECT STUFF(a.Col.query('c[position()>sql:column("b.SNum") and position()<=sql:column("b.ENum")]').value('.',
'varchar(50)'),
1, 1, '') AS Col
FROM ( SELECT CONVERT(XML, '<c>|' + REPLACE(@Str, '|',
'</c><c>|') + '</c>') AS Col
) AS a
CROSS JOIN ( SELECT b.number * 5 AS SNum ,
( b.number + 1 ) * 5 AS ENum
FROM master.dbo.spt_values AS b
WHERE b.type = 'P'
AND b.number < CEILING(( LEN(@Str)
- LEN(REPLACE(@Str,
'|', '')) )
/ 5.0)
) AS b
);
GO
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10')
/*
1|2|张三|王二麻子|5
6|7|8|9|10
*/
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10'执行不了,函数里有问题
#9
SELECT * FROM fn_SplitTab(N'1|2|张三|王二麻子|5|6|7|8|9|10'执行不了,函数里有问题