SQL Function to split string and concat substrings

萝らか妹 提交于 2020-04-18 06:13:13

问题


I am trying to create a function that will take in a column from a table, for each string in a cell it will split in to separate words and concat each word separatley to create different substrings. I have been looking at how to manage it with SQL like arrays et but I have had no luck

The two tables are :

Account(AccountID(PK), Name, Country)
accountSubstring(subID(PK), AccountID, Substring)

Ideally I need a function that would take in the column 'Name' from Account. For each row it would split the strings in to separate words and then concat each word through the string separatley. These newly created substrings would then be inputted to the accountSubstring table as 'Substring' along with the AccountID the substring belongs to from the original table.

To explain it better, for example, If one of the names in 'Name' was 'Stack Overflow Ireland Ltd', the expected outcome would be:

Stack
StackOverflow
StackOverflowIreland
StackOverflowIrelandLtd
Overflow
OverflowIreland
OverflowIrelandLtd
Ireland
IrelandLtd
Ltd

I would need to loop through each newly created split word and concat until it reaches the final word. Each of the above would be inserted into accountSubstring as a new row but with the same AccountID as it came from the same original string.

The Account table has thousands of rows and it will need to be done on all. It is a matching service and the substrings are used for comparison.

Hopefully this is clear as to what I am trying to do, Thanks very much in advance.


回答1:


This is adapting your previous question to use tables instead of constants. Here is an example:

with words as (
      select a.accountid, s.value as word, row_number() over (partition by a.accountid order by charindex(s.value, a.name)) as seqnum
      from account a cross apply
           string_split(a.name, ' ') s
     ),
     cte as (
      select accountid, seqnum, convert(varchar(max), word) as combined, format(seqnum, '000') as seqnums
      from words w
      union all
      select cte.accountid, w.seqnum, concat(cte.combined, ' ', w.word), concat(seqnums, ':', format(w.seqnum, '000'))
      from cte join
           words w
           on cte.accountid = w.accountid and w.seqnum = cte.seqnum + 1
     )
insert into accountSubstring (AccountId, Substring)
    select cte.accountId, cte.combined
    from cte
    order by accountid, seqnums;

A db<>fiddle is here.




回答2:


First Create number table.

SELECT TOP (4000)
    n = ISNULL(CONVERT(integer, ROW_NUMBER() OVER (ORDER BY (SELECT NULL))), 0)
INTO dbo.tblNumber1
FROM sys.columns AS c
CROSS JOIN sys.columns AS c2


CREATE UNIQUE CLUSTERED INDEX cuq
ON dbo.tblNumber1 (n)
WITH (MAXDOP = 1, SORT_IN_TEMPDB = ON);

Then Create Split String function

CREATE FUNCTION [dbo].[DelimitedSplitString] (
    @pString VARCHAR(8000)
    ,@pDelimiter CHAR(1)
    )
RETURNS TABLE
    WITH SCHEMABINDING
AS
RETURN
WITH cteStart(N1) AS (
        SELECT 1

        UNION ALL

        SELECT t.number + 1
        FROM dbo.tblNumber1 t
        WHERE number < datalength(@pString)
            AND SUBSTRING(@pString, t.number, 1) = @pDelimiter
        )
    ,cteLen(N1, L1) AS (
        SELECT s.N1
            ,ISNULL(NULLIF(CHARINDEX(@pDelimiter, @pString, s.N1), 0) - s.N1, 8000)
        FROM cteStart s
        )

SELECT ItemNumber = ROW_NUMBER() OVER (
        ORDER BY l.N1
        )
    ,Item = SUBSTRING(@pString, l.N1, l.L1)
FROM cteLen l;

The Final Script,

    DECLARE  @Input table( id int identity(1,1),pString VARCHAR(2000)) 
insert into @Input values
('Stack Overflow Ireland Ltd')
,('Replace a sequential set of numbers with special character')

declare @pDelimiter CHAR(1)=' '

declare @t table (id int,itemNum int,Item varchar(50))
INSERT INTO @t
SELECT i.id
    ,ca.ItemNumber
    ,ca.Item
FROM @Input I
CROSS APPLY (
    SELECT ItemNumber
        ,Item
    FROM [dbo].[DelimitedSplitString](pString, @pDelimiter)
    ) ca;

WITH CTE
AS (
    SELECT *
    FROM @t t
        ,dbo.tblNumber1 n
    WHERE n.number <= t.itemNum
    )
SELECT id
    ,(
        SELECT '' + item
        FROM cte c1
        WHERE c.id = c1.id
            AND c.number = c1.number
            AND c1.itemNum <= c.itemNum
        FOR XML path('')
        )
FROM CTE c
ORDER BY id
    ,number
    ,itemNum


来源:https://stackoverflow.com/questions/61041341/sql-function-to-split-string-and-concat-substrings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!