gpt4 book ai didi

sql-server - SQL Server 字符串到 m x n 表

转载 作者:行者123 更新时间:2023-12-03 23:39:30 25 4
gpt4 key购买 nike

这里有很多关于在 SQL 中拆分字符串的优秀帖子。
然而,我正在寻找一个真正的字符串到表函数。
换句话说,取一个表示表(具有多行和多列)的字符串并在 SQL Server 中生成等效的表。
这意味着有一个内部分隔符和一个外部分隔符。
字符串需要被外层分隔符分割成行,然后每一行变成一组列。
我在这里找不到任何发布的内容,所以我想我会提交我的版本并征求反馈。
在我的特定使用场景中,我知道每行不超过 10 列,并且总大小在 varchar(8000) 之下。
这是 tfSplitString 函数(列出只是为了完整性;您可以根据需要使用自己的版本),然后是 dbo.tfStringToTable10Cols 。由于我不知道列名,它们只是输出为 Col1、Col2、Col3 等。

if object_id('dbo.tfSplitString') is not null 
exec (' DROP FUNCTION [dbo].[tfSplitString]');
go

CREATE FUNCTION [dbo].[tfSplitString]
/**********************************************************************************************************************
Purpose:
Split a given string at a given delimiter and return a list of the split elements (items).

Notes:
1. Leading a trailing delimiters are treated as if an empty string element were present.
2. Consecutive delimiters are treated as if an empty string element were present between them.
3. Except when spaces are used as a delimiter, all spaces present in each element are preserved.

select * from [dbo].[tfSplitString]('This is a test of the emergency breadcast system', ' ')
Returns:
1 This
2 is
3 a
4 test
5 of
6 the
7 emergency
8 breadcast
9 system

iTVF containing the following:
ItemNumber = Element position of Item as a BIGINT (not converted to INT to eliminate a CAST)
Item = Element value as a VARCHAR(8000)


CROSS APPLY Usage Examples and Tests:
--=====================================================================================================================
-- TEST 1:
-- This tests for various possible conditions in a string using a comma as the delimiter. The expected results are
-- laid out in the comments
--=====================================================================================================================
--===== Conditionally drop the test tables to make reruns easier for testing.
-- (this is NOT a part of the solution)
IF OBJECT_ID('tempdb..#JBMTest') IS NOT NULL DROP TABLE #JBMTest
;
--===== Create and populate a test table on the fly (this is NOT a part of the solution).
-- In the following comments, "b" is a blank and "E" is an element in the left to right order.
-- Double Quotes are used to encapsulate the output of "Item" so that you can see that all blanks
-- are preserved no matter where they may appear.
SELECT *
INTO #JBMTest
FROM ( --# & type of Return Row(s)
SELECT 0, NULL UNION ALL --1 NULL
SELECT 1, SPACE(0) UNION ALL --1 b (Empty String)
SELECT 2, SPACE(1) UNION ALL --1 b (1 space)
SELECT 3, SPACE(5) UNION ALL --1 b (5 spaces)
SELECT 4, ',' UNION ALL --2 b b (both are empty strings)
SELECT 5, '55555' UNION ALL --1 E
SELECT 6, ',55555' UNION ALL --2 b E
SELECT 7, ',55555,' UNION ALL --3 b E b
SELECT 8, '55555,' UNION ALL --2 b B
SELECT 9, '55555,1' UNION ALL --2 E E
SELECT 10, '1,55555' UNION ALL --2 E E
SELECT 11, '55555,4444,333,22,1' UNION ALL --5 E E E E E
SELECT 12, '55555,4444,,333,22,1' UNION ALL --6 E E b E E E
SELECT 13, ',55555,4444,,333,22,1,' UNION ALL --8 b E E b E E E b
SELECT 14, ',55555,4444,,,333,22,1,' UNION ALL --9 b E E b b E E E b
SELECT 15, ' 4444,55555 ' UNION ALL --2 E (w/Leading Space) E (w/Trailing Space)
SELECT 16, 'This,is,a,test.' --E E E E
) d (SomeID, SomeValue)
;
--===== Split the CSV column for the whole table using CROSS APPLY (this is the solution)
SELECT test.SomeID, test.SomeValue, split.ItemNumber, Item = QUOTENAME(split.Item,'"')
FROM #JBMTest test
CROSS APPLY dbo.tfSplitString(test.SomeValue,',') split
;
--=====================================================================================================================
-- TEST 2:
-- This tests for various "alpha" splits and COLLATION using all ASCII characters from 0 to 255 as a delimiter against
-- a given string. Note that not all of the delimiters will be visible and some will show up as tiny squares because
-- they are "control" characters. More specifically, this test will show you what happens to various non-accented
-- letters for your given collation depending on the delimiter you chose.
--=====================================================================================================================
WITH
cteBuildAllCharacters (String,Delimiter) AS
(
SELECT TOP 256
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
CHAR(ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1)
FROM master.sys.all_columns
)
SELECT ASCII_Value = ASCII(c.Delimiter), c.Delimiter, split.ItemNumber, Item = QUOTENAME(split.Item,'"')
FROM cteBuildAllCharacters c
CROSS APPLY dbo.tfSplitString(c.String,c.Delimiter) split
ORDER BY ASCII_Value, split.ItemNumber
;
--===== Define I/O parameters

*/
(@pString VARCHAR(8000), @pDelimiter CHAR(1))
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
with
cte1(prevPos, nextPos) as ( -- previous position of the delimiter; next position of the delimiter
select 0, CHARINDEX(@pDelimiter, @pString)
union all
select nextPos, CHARINDEX(@pDelimiter, @pString, nextPos+1)
from cte1
where nextPos>0
)
select row_number() over (order by (select null)) ItemNumber
, SUBSTRING(@pString, prevPos+1, case when nextPos=0 then len(@pString) else nextPos-(prevPos+1) end) Item
from cte1;
GO

if object_id('dbo.tfStringToTable10Cols') is not null exec (' DROP FUNCTION [dbo].[tfStringToTable10Cols]');

go

Schema_drop_function 'tfStringToTable10Cols'
go

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
/**
Inlined Table-valued function to deserialize a string into an n by 10 table.
Column values must not contain either the inner or outer delimiter.
Empty rows are ignored.
Note: This could trivially be converted to return more columns if a use case arises

Example:
select * from dbo.tfStringToTable20Cols('a1,b1,c1,d1,e1,f1,g1,h1,i1,j1;;,b2,c2;a3,,c3,d3,e3,f3,g3,h3,i3,j3', DEFAULT, DEFAULT)
should return

Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10
a1 b1 c1 d1 e1 f1 g1 h1 i1 j1
b2 c2 NULL NULL NULL NULL NULL NULL NULL
a3 c3 d3 e3 f3 g3 h3 i3 j3

*/
create function dbo.tfStringToTable10Cols
(
@Series varchar(8000),
@OuterDelimiter char(1) = ';',
@InnerDelimiter char(1) = ','
)
RETURNS TABLE
AS
RETURN
select p.[1] Col1, p.[2] Col2, p.[3] Col3, p.[4] Col4, p.[5] Col5, p.[6] Col6, p.[7] Col7, p.[8] Col8, p.[9] Col9, p.[10] Col10
from dbo.tfSplitString(@Series, @OuterDelimiter) as r -- This breaks up the strings into serialized rowstrings
cross apply (
select * from dbo.tfSplitString(r.Item, @InnerDelimiter) cols -- This breaks up the rowstrings into columnNumber and columnVal rows
pivot (Max(cols.Item) -- The pivot then turns these rows into columns. The Max() is a no-op since each columnNumber is unique
for cols.ItemNumber in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10]))
as tmp) as P
where len(r.Item)>0
go


select * from dbo.tfStringToTable10Cols('a1,b1,c1,d1,e1,f1,g1,h1,i1,j1;,b2,c2;a3,,c3,d3,e3,f3,g3,h3,i3,j3', DEFAULT, DEFAULT)```

最佳答案

只是为了好玩。
如果您碰巧在 2016 年以上,您可以利用 JSON 功能,并远离您的 tfSplitString功能
示例

Declare @Series         varchar(max) = 'a1,b1,c1,d1,e1,f1,g1,h1,i1,j1;;,b2,c2;a3,,c3,d3,e3,f3,g3,h3,i3,j3'
Declare @OuterDelimiter varchar(25) = ';'
Declare @InnerDelimiter varchar(25) = ','

Select B.*
From (
Select RetSeq = [Key]+1
,RetVal = Value
From OpenJSON( '["'+replace(replace(@Series,'"','\"'),@OuterDelimiter,'","')+'"]' )
) A
Cross Apply (
Select Pos1 = JSON_VALUE(S,'$[0]')
,Pos2 = JSON_VALUE(S,'$[1]')
,Pos3 = JSON_VALUE(S,'$[2]')
,Pos4 = JSON_VALUE(S,'$[3]')
,Pos5 = JSON_VALUE(S,'$[4]')
,Pos6 = JSON_VALUE(S,'$[5]')
,Pos7 = JSON_VALUE(S,'$[6]')
,Pos8 = JSON_VALUE(S,'$[7]')
,Pos9 = JSON_VALUE(S,'$[8]')
,Pos10 = JSON_VALUE(S,'$[9]')
From ( values ( '["'+replace(replace(A.RetVal,'"','\"'),@InnerDelimiter,'","')+'"]' ) ) A(S)
) B
Where A.RetVal <>''
返回
Pos1    Pos2    Pos3    Pos4    Pos5    Pos6    Pos7    Pos8    Pos9    Pos10
a1 b1 c1 d1 e1 f1 g1 h1 i1 j1
b2 c2 NULL NULL NULL NULL NULL NULL NULL
a3 c3 d3 e3 f3 g3 h3 i3 j3

关于sql-server - SQL Server 字符串到 m x n 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66592729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com