当前位置:网站首页>The SQL implementation divides the values in the fields in the data table into multiple columns according to the separator
The SQL implementation divides the values in the fields in the data table into multiple columns according to the separator
2022-07-18 05:28:00 【No envy】
List of articles
SQL Realize dividing the values in the fields in the data table into multiple columns according to the separator
The original form :

Job requirements : Add the class_all The data of the field has been divided by semicolons , Generate the following table :

1. Build table
Build table SQL:
CREATE TABLE [dbo].[test_table](
[id] nvarchar(max) NOT NULL,
[class_all] nvarchar(max) NULL
)
2. insert data
insert into test_table VALUES ('1','111;222;333');
insert into test_table VALUES ('2','444;555');
insert into test_table VALUES ('3','');
3. Demand analysis
according to class_all The field has been divided by semicolons , Then group by category , And the front is corresponding id
4. Implementation code
It just needs to be modified Table name , Query field 1, Split fields , Result field name , Separator that will do !!!
WITH Table name
AS
(
SELECT
Query field 1,
Result field name = CAST(LEFT(REPLACE( Split fields , ' ', ''),CHARINDEX(' Separator ',REPLACE( Split fields ,' ', '') + ' Separator ')-1) AS NVARCHAR(MAX)) ,
Split = CAST(STUFF(REPLACE( Split fields , ' ', '') + ' Separator ',1,CHARINDEX(' Separator ',REPLACE( Split fields , ' ','') + ' Separator '), '') AS NVARCHAR(MAX))
FROM dbo. Table name
WHERE Split fields IS NOT NULL
UNION ALL
SELECT
Query field 1,
Result field name = CAST(LEFT(Split,CHARINDEX(' Separator ', Split) - 1) AS NVARCHAR(MAX)) ,
Split = CAST(STUFF(Split, 1, CHARINDEX(' Separator ', Split), '') AS NVARCHAR(MAX))
FROM Table name
WHERE Split > ''
)
SELECT Query field 1, Result field name FROM Table name
5. Case code
WITH test_table -- Table name
AS
(
SELECT
id,
class = CAST(LEFT(REPLACE(class_all, ' ', ''),CHARINDEX(';',REPLACE(class_all,' ', '') + ';')-1) AS NVARCHAR(MAX)) ,
Split = CAST(STUFF(REPLACE(class_all, ' ', '') + ';',1,CHARINDEX(';',REPLACE(class_all, ' ','') + ';'), '') AS NVARCHAR(MAX))
FROM dbo.test_table
WHERE class_all IS NOT NULL
UNION ALL
SELECT
id,
class = CAST(LEFT(Split,CHARINDEX(';', Split) - 1) AS NVARCHAR(MAX)) ,
Split = CAST(STUFF(Split, 1, CHARINDEX(';', Split), '') AS NVARCHAR(MAX))
FROM test_table
WHERE Split > ''
)
SELECT id,class FROM test_table
6. Realization effect
The effect is as follows :

边栏推荐
猜你喜欢

一文搞懂│什么是跨域?如何解决跨域?

直播带货系统源码

【动态规划】—— 数位统计DP

Volatile low configuration syn, realizing visibility and order

蚂蚁隐私计算创新TEE技术获学术认可

freeswitch的话单模块

关于安全细节 Timing Attack

About security details timing attack

DBeaver连接mysql错误:The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognized or represents more than

在Salesforce中基于SAML 2.0搭建SSO并启用JIT User Provisioning(SF Orgs间 / SF Org与Experience Cloud间 / 其他IdP)
随机推荐
SQL实现将数据表中的字段中的值按分隔符分成多列
CAS Compare and Swap 比较后交换
触发器创建删除等操作
The difference between sx126x and sx127x
Doxygen的安装与使用及注释语法
【7.8-7.15】写作社区精彩技术博文回顾
Salesforce中實施Campaign Influence模型注意事項
Nifi listsftp intensive talk
Implementation of CRC16 verification C language
MySQL 添加用户并授予只能查询权限
Live delivery system source code
Shell编程基入门
网络安全网格概念以及特点简单普及
Alibaba cloud e-mapreduce geek competition is open for registration. Hundreds of thousands of bonuses are waiting for you to challenge
Introduction to STM32 IO port mode
Information system project managers must memorize the core examination points (III) 14 graphic tools of UML
代码合规性:开发人员使用Helix QAC的5大原因
【用户文章】P4合并实践指南之实例拆解Resolve
Do you know the debugging skills of the most commonly used Chrome browser console.
CRC16校验 C语言实现