SQL SERVER: HOW TO SPLIT OR CONVERT DELIMITED STRING INTO ROWS WITHOUT USING FUNCTION

SQL SERVER: HOW TO SPLIT OR CONVERT DELIMITED STRING INTO ROWS WITHOUT USING FUNCTION
Introduction: In this article I am going to explain How to convert or split the delimited string (comma separated string) into table rows by converting them into xml and then parsing the xml to split them in rows.

In previous articles I have explained How to Convert or split comma separated string into table rows in sql server and Get column values as comma separated list in sql server and Split string from comma and get left and right part and Concatenate rows values as a comma separated string using for xml path and stuff and Separate integer and fractional part from decimal number

Implementation: Let’s create a table and add dummy data using the following script

CREATE TABLE tbItems
(
Category VARCHAR(100),
Items VARCHAR(MAX)
)
GO
INSERT INTO tbItems(Category, Items)
VALUES
(‘Dairy Products’,‘Cheese,Milk,Yogurt’),
(‘Vegetables’,‘Onion,Tomato’),
(‘Fruits’,‘Apple,Banana,Orange’)

View table data
GO
SELECT * FROM tbItems

Output will be:

在这里插入图片描述

Query to split delimited string

GO
SELECT Category, col.value(‘.’,‘VARCHAR(MAX)’) AS Items
FROM
(
SELECT Category, CAST(‘’ + REPLACE(Items, ‘,’ , ‘’) + ‘ ’ AS XML) AS CategoryItems
FROM tbItems
) AS tb
CROSS APPLY CategoryItems.nodes(’/Items/item’) tab(col)

Result will be:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值