问题
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