I have a table (Table A) that looks like this:
Id (int)
Quantity (int)
Clicks (int?)
Opens (int?)
VendorName (string)
I want to convert it to a table (Table B) like this
Id (int)
JsonData (Json that has the 4 fields above)
How do you insert/transform into a json column? Or do I just have to construct the json string myself?
Additionally, does the nullableness of the int
columns affect creation? Are clicks
for example not included or do they do something like this {"clicks": null }
?
I read through Microsoft's documentation about JSON, but I didn't see any reference to inserting from SQL columns. Only from other JSON data.
2条答案
按热度按时间dldeef671#
For SQL Server 2016+ you may try a combination of
FOR JSON PATH
withWITHOUT_ARRAY_WRAPPER
andINCLUDE_NULL_VALUES
.Starting from SQL Server 2022, you may use
JSON_OBJECT()
function.Table:
Statement for SQL Server 2016+:
Statement for SQL Server 2022+:
Result:
ddrv8njm2#
Something like this