json - How to parse BSON date (unix epoch) using OPENJSON in SQL Server -
i want insert json document (created python client json.dumps(payload, default=json_util.default))) db. have following procedure in sql server (azure).
create procedure insertreading(@reading nvarchar(max)) begin insert readings(idmongo, value, name, date) select _id, value, name, date openjson (@reading) (_id nvarchar(50), value float, date datetime, name nvarchar(50)) end it works fine when execute data (not document have - testing only):
exec insertreading '{ "value" : 21.625, "name" : "myname", "date" : "2016-03-30t07:38:27.102z" }' but fails with
exec insertreading '{"date": {"$date": 1503074335547}, "value": 23.5, "name": "myname"}' error (date conversion fails , returns null):
cannot insert value null column 'date', table 'db.dbo.readings'; column not allow nulls. insert fails. how insert json correctly? preferably without changing date format in client (i'm sending simultaneously mongo , sql).
yeah, that's no fun. sql server has no support format, have construct date manually.
select _id, [value], [name], coalesce( [date], dateadd(millisecond, [datemillis] % 1000, dateadd(second, [datemillis] / 1000, '19700101')) ) openjson (@reading) (_id nvarchar(50), [value] float, [date] datetime, [datemillis] bigint '$.date."$date"', [name] nvarchar(50)) this allow pass both formats (date/time literals , millisecond offsets).
Comments
Post a Comment