Datetime conversion from mongoDB string to SQL

為{幸葍}努か 提交于 2021-02-04 08:27:27

问题


I am trying to insert record from MongoDB to MSSQL database.

createdAt field in mongoDB is a mixture of datetime and string values for some reason.

so some records are like = "createdAt" : ISODate("2020-01-17T23:45:00.000Z") and some records are like = "createdAt" : "2020-04-18T07:00:00Z"

Here is my stored procedure :

ALTER PROCEDURE [dbo].[spInsertInOutAggregate]
(@passingInOutAggregate dbo.tempInOutAggregateData1 READONLY)

AS 
BEGIN 

MERGE dbo.inoutaggregatedata AS target
USING @passingInOutAggregate AS source

ON (target._id = source._id)

WHEN NOT MATCHED THEN

INSERT (_id, place, totalIn, totalOut, sensorNumber, createdAt)
VALUES (source._id, source.place, source.totalIn, source.totalOut, 
source.sensorNumber, 
CAST(source.createdAt AS DATETIME2),
--CASE WHEN Isnumeric(source.createdAt) = 1
--THEN DATEADD(S, CONVERT(int,LEFT(source.createdAt, 10)), '1970-01-01') ELSE CONVERT(datetime, source.createdAt) END
);
-- DATEADD(S, CONVERT(int,LEFT(source.createdAt, 10)), '1970-01-01'));
END

I am able to convert records with ISODate("2020-01-17T23:45:00.000Z") to datetime using CASE by doing DATEADD(S, CONVERT(int,LEFT(source.createdAt, 10)), '1970-01-01')

but for records where date time is a string, i always get NULL in SQL database.

来源:https://stackoverflow.com/questions/63037209/datetime-conversion-from-mongodb-string-to-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!