[英]Split function in T-SQL
I have table with field that contains coma separated string. 我有带有包含逗号分隔字符串的字段的表。 I whant create query thats return follow result:
我想创建查询,即返回跟随结果:
I want to create query that return split text. 我想创建返回拆分文本的查询。 For exapmle:
例如:
Data in table: 表中数据:
| ID | Names |
| ------------------------------------ | -------------------------------- |
| 63F5D993-3AC9-4EEA-8007-B669542BAD9A | John Smith,Kerry King,Tom Arraya |
Required result: 所需结果:
ID | Names
------------------------------------ | -----------
63F5D993-3AC9-4EEA-8007-B669542BAD9A | John Smith
------------------------------------- | -----------
63F5D993-3AC9-4EEA-8007-B669542BAD9A | Kerry King
------------------------------------- | -----------
63F5D993-3AC9-4EEA-8007-B669542BAD9A | Tom Arraya
I found "split" function for T-SQL but its works not quite right for my case. 我发现了T-SQL的“拆分”功能,但是对于我的情况,它的工作效果并不理想。 I can't execute it like this:
我不能像这样执行它:
SELECT dbo.Split(dbo.name, ',') FROM dbo.Mytable
It can execute only follows: 它只能执行以下操作:
SELECT * FROM dbo.Split('John Smith,Kerry King,Tom Arraya', ',')
But it does not suit me. 但这不适合我。 Also i attempted write cursor:
我也尝试写游标:
DECLARE @bkb varchar(256)
DECLARE @Bkb_Cursor CURSOR
SET @Bkb_Cursor = CURSOR SCROLL FOR
SELECT bkb.best_know_by
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
left JOIN [dbo].[email_addr_bean_rel] eb
ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea
ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs
ON eb.bean_id = ccs.id_c
left JOIN [dbo].[BestKnowBy$] bkb
ON c.[campaign_id] =bkb.Con_id
where c.deleted = 0;
OPEN @Bkb_Cursor;
FETCH NEXT FROM @Bkb_Cursor
INTO @bkb;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT dbo.Splitfn(@bkb);
FETCH NEXT FROM @Bkb_Cursor INTO @bkb;
END
CLOSE @Bkb_Cursor;
DEALLOCATE @Bkb_Cursor;
GO
But it is not worked. 但这是行不通的。 I get error "Column "dbo" is not allowed in this context, and the user-defined function or aggregate "dbo.Splitfn" could not be found."
我收到错误“在此情况下不允许使用列“ dbo”,并且找不到用户定义的函数或聚合“ dbo.Splitfn”。”
How can I solve this problem? 我怎么解决这个问题?
My query looks like follow: 我的查询如下所示:
SELECT c.[id],
bkb.best_know_by
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
left JOIN [dbo].[email_addr_bean_rel] eb
ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea
ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs
ON eb.bean_id = ccs.id_c
left JOIN [dbo].[BestKnowBy$] bkb
ON c.[campaign_id] =bkb.Con_id
where c.deleted = 0;
The bkb.best_know_by field contains comma separated string. bkb.best_know_by字段包含逗号分隔的字符串。 How can i use "Cross Apply" in this case?
在这种情况下,如何使用“交叉申请”?
You need to use CROSS APPLY
in combination with your table's result set which will execute the function for every row: 您需要结合使用
CROSS APPLY
和表的结果集,该结果集将对每一行执行该函数:
SELECT st.ID, spl.value
FROM SplitTest st
CROSS APPLY string_split(st.Names, ',') spl
Edit: With regard to the addition of your query to your question, you could do the following: 编辑:关于您的查询添加到您的问题,您可以执行以下操作:
;WITH CTE_Query AS (
SELECT c.[id],
bkb.best_know_by
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
left JOIN [dbo].[email_addr_bean_rel] eb
ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea
ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs
ON eb.bean_id = ccs.id_c
left JOIN [dbo].[BestKnowBy$] bkb
ON c.[campaign_id] =bkb.Con_id
where c.deleted = 0
)
SELECT cte.id, spl.value
FROM CTE_Query AS cte
CROSS APPLY string_split(cte.best_know_by, ',') spl
Cross Apply will do the trick 交叉应用将解决问题
Select A.ID
,Names = B.Item -- << Return Field from your Split Function
From YourTable A
Cross Apply (Select * from dbo.Split(A.Names, ',') ) B
With your query
与您的查询
SELECT c.[id]
,S.* --<< Removed bkb.best_know_by and Replaced with S.* (don't know your Split() Return Field)
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
LEFT JOIN [dbo].[email_addr_bean_rel] eb ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs ON eb.bean_id = ccs.id_c
LEFT JOIN [dbo].[BestKnowBy$] bkb ON c.[campaign_id] =bkb.Con_id
Cross Apply dbo.Split(bkb.best_know_by,',') S
where c.deleted = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.