简体   繁体   中英

How to add a new property in json structure existing in nvarchar(max) type column in SQL Server

I have data like this in an employee table in a column of type nvarchar(max) :

{
   "ID":101,
   "FirstName":"Vatan",
   "LastName":"Soni"
}

Now I need to update all rows with FullName property as well, so result should be

{
    "ID":101,
    "FirstName":"Vatan",
    "LastName":"Soni",
    "FullName":"Vatan Soni"
}

Please help and provide the SQL update script for it.

Thanks in advance.

Starting with SQL 2016 you have JSON types and functions: JSON_MODIFY :

UPDATE <table> 
  SET <column> = JSON_MODIFY(<column>, '$.FullName', 
  JSON_VALUE(<column>, '$.FirstName') + ' ' + JSON_VALUE(<column>, '$.LastName'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM