久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用于拆分字符串和連接子字符串的 SQL 函數

SQL Function to split string and concat substrings(用于拆分字符串和連接子字符串的 SQL 函數)
本文介紹了用于拆分字符串和連接子字符串的 SQL 函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試創建一個函數,該函數將從表中接收一列,對于單元格中的每個字符串,它將拆分為單獨的單詞并將每個單詞連接起來以創建不同的子字符串.我一直在研究如何使用像數組等的 SQL 來管理它,但我沒有運氣

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

這兩個表是:

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

理想情況下,我需要一個函數來接收來自帳戶的名稱"列.對于每一行,它會將字符串拆分為單獨的單詞,然后通過字符串 separatley 連接每個單詞.然后,這些新創建的子字符串將作為子字符串"連同子字符串所屬的原始表中的 AccountID 輸入到 accountSubstring 表中.

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.

為了更好地解釋它,例如,如果Name"中的名稱之一是Stack Overflow Ireland Ltd",則預期結果將是:

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

我需要遍歷每個新創建的拆分詞并連接到最后一個詞.上面的每一個都將作為一個新行插入到 accountSubstring 中,但具有相同的 AccountID,因為它來自相同的原始字符串.

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.

Account 表有數千行,需要對所有行進行處理.它是一個匹配服務,使用子字符串進行比較.

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.

推薦答案

首先創建數字表.

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;

最終劇本,

    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

這篇關于用于拆分字符串和連接子字符串的 SQL 函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 国产丝袜一区二区三区免费视频 | 九九综合| 亚洲在线一区 | 日韩视频在线一区二区 | 毛片在线免费 | 国产精品高潮呻吟 | 欧美一区二区三区在线看 | 久久精品国产亚洲一区二区 | 日韩一区精品 | 欧美精品网站 | 天天操夜夜操 | 九九免费视频 | 欧美日韩精品一区二区天天拍 | 国产成人a亚洲精品 | 国产黄色小视频在线观看 | 亚洲国产精品日本 | 一区二区三区四区在线视频 | 天天色天天射天天干 | 亚洲一区二区三区免费在线观看 | 麻豆一区二区三区 | 日韩一区和二区 | 99精品国产一区二区三区 | 日韩91 | 在线视频日韩精品 | 亚洲成人免费电影 | 色欧美日韩 | 久久精品一二三影院 | 国产精品大片 | 亚洲国产精品久久久 | 亚洲高清在线免费观看 | 久久精品成人 | 作爱视频免费观看 | 日韩av一区二区在线观看 | 成人二区 | www国产亚洲精品久久网站 | 日本在线精品视频 | 一区二区视频免费观看 | 国产日产欧产精品精品推荐蛮挑 | 国产一区二区三区久久 | 欧美欧美欧美 | 欧美一区二区三区的 |