browsw
asked on
Hyphen in JSON data causes OPENJSON to fail on SQL 2016
I'm trying to pull some data from an API with JSON into an SQL 2016 DB. However if the data in one of the fields contains a hyphen it causes the query to fail. How do I solve this?
Thank you
Code:
Thank you
Code:
declare @JSON nvarchar(max) = '[{"id":1,"preferredName":"James","surname":"Brown-Smith"}]'
Merge INTO TblStaff as TARGET
using(SELECT * FROM OPENJSON(@json) WITH (
surname NVARCHAR(MAX) '$.surname',
preferredName NVARCHAR(MAX) '$.preferredName',
id INT '$.id'
)) as SOURCE on TARGET.intStaffID=SOURCE.id when matched then update set TARGET.txtSurname=SOURCE.surname, TARGET.txtPrename=SOURCE.preferredName, TARGET.intStaffID=SOURCE.id
WHEN NOT MATCHED BY TARGET THEN insert (txtSurname, txtPrename, intStaffID) values (SOURCE.surname,SOURCE.preferredName,SOURCE.id) ;
ASKER
Thank you, you pointed me towards the problem which was an incorrect data type on the existing table for txtSurname.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Cause when I craft one it works as intended:
Open in new window