sqlserver使用cte实现某列按字符分隔成多行
时间:2021-07-01 10:21:17
帮助过:92人阅读
roy
AS
(SELECT
[DataRowStartIndex] ,
[DataRowEndIndex] ,
[DataColumnStartIndex] ,
[DataColumnEndIndex] ,
[Separator],
[SheetName]=CAST(
left(
[SheetName],
CHARINDEX(
‘,‘,
[SheetName]+‘,‘)
-1)
AS nvarchar(
100)),
Split=CAST(
STUFF(
[SheetName]+‘,‘,
1,
CHARINDEX(
‘,‘,
[SheetName]+‘,‘),
‘‘)
AS NVARCHAR(
100))
FROM AccessTypeForExcel
UNION ALL
SELECT
[DataRowStartIndex] ,
[DataRowEndIndex] ,
[DataColumnStartIndex] ,
[DataColumnEndIndex] ,
[Separator],
[SheetName]=CAST(
left(Split,
CHARINDEX(
‘,‘,Split)
-1)
AS NVARCHAR(
100)),
Split= CAST(
STUFF(Split,
1,
CHARINDEX(
‘,‘,Split),
‘‘)
AS NVARCHAR(
100))
FROM Roy
WHERE split
>‘‘)
SELECT
[DataRowStartIndex] ,
[DataRowEndIndex] ,
[DataColumnStartIndex] ,
[DataColumnEndIndex] ,
[Separator],
[SheetName]
FROM roy
OPTION (MAXRECURSION
0)
这个就是按照","分割字符串,
每次分成第一个逗号前([sheetname])的和之后的部分(split),
然后自联,
拿上次余下的部分(split)继续重复前次的分割操作,
直到不满足split>‘‘这个条件时,一条数据就算分割好了
sqlserver使用cte实现某列按字符分隔成多行
标签:sqlserver recursion art arch weight div cte 部分 就是