Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove JSON property stored in a SQL Server column?

I have a task to remove a JSON property saved in a SQL Server database table's column. Here is the structure of my table

OrderId   Name   JSON

In the JSON column I have this JSON data:

{ 
    "Property1" :"",
    "Property2" :"",
    "metadata-department": "A",
    "metadata-group": "B"
}

I have over 500 records that have this json value.

Can I update all the records to remove metadata-department?

like image 678
user123456 Avatar asked Apr 28 '15 11:04

user123456


People also ask

How do I get rid of JSON?

To remove JSON element, use the delete keyword in JavaScript.

How do I update a JSON column in SQL?

You can use the UPDATE statement to modify values of a JSON column in the SET clause. You can only update the JSON column, not individual portions of the JSON instance. Also, when referencing a JSON column in its entirety, the input format is the same as the INSERT statement.

Is it OK to store JSON in SQL database?

You can store JSON documents in SQL Server or SQL Database and query JSON data as in a NoSQL database.

What is a drawback of JSON columns SQL?

The drawback? If your JSON has multiple fields with the same key, only one of them, the last one, will be retained. The other drawback is that MySQL doesn't support indexing JSON columns, which means that searching through your JSON documents could result in a full table scan.


1 Answers

This question is old but I thought I'd post another solution for those who may end up here via search engine.

In SQL Server 2016 or SQL Azure, the following works:

DECLARE @info NVARCHAR(100) = '{"name":"John","skills":["C#","SQL"]}'

PRINT JSON_MODIFY(@info, '$.name', NULL)

-- Result: {"skills":["C#","SQL"]}

Source: https://msdn.microsoft.com/en-us/library/dn921892.aspx

like image 190
Dennis W Avatar answered Sep 21 '22 23:09

Dennis W