There are scores of excellent posts here related to splitting a string in SQL. I am looking however for a true string to table function.
In other words, taking a string that represents a table (with multiple rows and columns) and generating the equivalent table in SQL Server.
This means that there is an inner delimiter, and an outer delimiter.
The string needs to be split by the outer delimiter into rows, and then each row turned into a set of columns.
I have not been able to find anything posted here, so I thought I'd submit my version and solicit feedback.
In my particular usage scenarios, I know that there are no more than 10 columns per row, and that the total size is under varchar(8000).
Here's the tfSplitString function (listed just for completeness; you can use your own version as you like) followed by the dbo.tfStringToTable10Cols. Since I don't know the column names, they are simply output as Col1, Col2, Col3, etc.
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)```