使用 JSON 数组索引 0 或子字符串更新单元格

Update cell with JSON array index 0 or with sub-string

我的单元格中有一个 JSON 数组,我需要通过删除数组中的第二项来更新该单元格。或者,如果您将其视为字符串,我需要通过删除部分字符串来更新单元格。例如:

原创

[{"ID":1}, {"ID":2}]

更新

[{"ID":1}] 

Table

因为你不是 2016+,你可以做一些字符串操作。

例子

Declare @YourTable Table ([ID] varchar(50),[Column1] varchar(50))
Insert Into @YourTable Values 
 (1,'[{"ID":1}, {"ID":2}]')
,(2,'[{"ID":2}]') 

Select * 
      ,NewValue = replace(left(Column1,charindex(',',Column1+',')-1)+']',']]',']')
 From  @YourTable

Returns

ID  Column1                 NewValue
1   [{"ID":1}, {"ID":2}]    [{"ID":1}]
2   [{"ID":2}]              [{"ID":2}]