[英]Split a column in T-SQL
I'm new to using T-SQL, how do you separate an NVARCHAR column with the following information我是使用 T-SQL 的新手,如何用以下信息分隔 NVARCHAR 列
[3293,"Maria","CA","Auto"]
[67093,"Joana","WA","Manual"]
, ,
I would like to get 4 columns like this我想得到这样的 4 列
col1 col2 col3 col4
3293 Maria CA Auto
67093 Joana WA Manual
Thanks谢谢
You can use openjson
and aggregate:您可以使用openjson
和聚合:
select
max(case when [key] = 0 then value end) col1,
max(case when [key] = 1 then value end) col2,
max(case when [key] = 2 then value end) col3,
max(case when [key] = 3 then value end) col4
from OpenJson('[3293,"Maria","CA","Auto"]')
Without the need for an aggregate无需聚合
Example例子
Select Col1 = JSON_VALUE([SomeCol],'$[0]')
,Col2 = JSON_VALUE([SomeCol],'$[1]')
,Col3 = JSON_VALUE([SomeCol],'$[2]')
,Col4 = JSON_VALUE([SomeCol],'$[3]')
From YourTable A
Results结果
Col1 Col2 Col3 Col4
3293 Maria CA Auto
67093 Joana WA Manual
Use 'OpenJson' and pass the column values in a loop.使用“OpenJson”并循环传递列值。
--Step1: Create a Temporary table and add Row_Number
select ROW_NUMBER() over( order by COL) as r,*
INTO #Temp_table
from YourTable;
--Step2: Declare and set Variables
DECLARE @count INT;
DECLARE @row INT;
DECLARE @JSON NVARCHAR(250);
set @count= (select COUNT(1) FROM #Temp_table);
SET @row = 1;
--Step3: Create Final table (Here, using temp final table)
CREATE TABLE #TEMP_FINAL
(COL1 INT,COL2 VARCHAR(100),COL3 VARCHAR(100),COL4 VARCHAR(100));
--Step4: Iterate over loop
WHILE (@row <= @count) BEGIN
SELECT @JSON=COL FROM #Temp_table WHERE @row=r;
INSERT INTO #TEMP_FINAL
select
max(case when [key] = 0 then value end) col1,
max(case when [key] = 1 then value end) col2,
max(case when [key] = 2 then value end) col3,
max(case when [key] = 3 then value end) col4
from OpenJson(@JSON);
SET @row += 1;
END
--Step5: Select the values
SELECT * FROM #TEMP_FINAL
To have understanding, you can review following links要了解,您可以查看以下链接
Row_Number: https://www.sqlservertutorial.net/sql-server-window-functions/sql-server-row_number-function/ While Loop: https://www.sqlshack.com/sql-while-loop-with-simple-examples/行号: https://www.sqlservertutorial.net/sql-server-window-functions/sql-server-row_number-function/ While 循环: https://www.sqlshack.com/sql-while-loop-with-simple -例子/
One more suggestion uses a trick to stuff a json array into another array.另一个建议使用一种技巧将 json 阵列填充到另一个阵列中。 This allows for a type-safe(!) and pivot/aggregate-free WITH
-clause:这允许使用类型安全(!)和无主元/聚合的WITH
子句:
Declare a dummy table to provide a showcase (please do this yourself in your next question).声明一个虚拟表来提供展示(请在下一个问题中自己做)。
DECLARE @dummyTable TABLE(ID INT IDENTITY, YourJson NVARCHAR(MAX));
INSERT INTO @dummyTable(YourJson) VALUES
('[3293,"Maria","CA","Auto"]')
,('[67093,"Joana","WA","Manual"]');
--the query --查询
SELECT t.ID
,JsonValues.*
FROM @dummyTable t
CROSS APPLY OPENJSON(CONCAT('[',t.YourJson,']'))
WITH
(
TheNumber int '$[0]'
,Firstname nvarchar(100) '$[1]'
,[State] nvarchar(100) '$[2]'
,[Type] nvarchar(100) '$[3]'
) JsonValues;
The idea in short:简而言之:
CONCAT()
we add a [
and a ]
around your array.使用CONCAT()
我们在数组周围添加[
和]
。WITH
specifying the resulting column with name, type and the json path to grab it.现在我们可以使用WITH
指定带有名称、类型和 json 路径的结果列来抓取它。The result:结果:
+----+-----------+-----------+-------+--------+
| ID | TheNumber | Firstname | State | Type |
+----+-----------+-----------+-------+--------+
| 1 | 3293 | Maria | CA | Auto |
+----+-----------+-----------+-------+--------+
| 2 | 67093 | Joana | WA | Manual |
+----+-----------+-----------+-------+--------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.