I'm currently importing some data from a very old database (access 2.0) into a sql server 2012 database. I imported all of the data (including the date field as it had values of 01.01.105 for 01.01.2005 ) into varchar fields.
Then I'm trying to import the data from the temporary table into the maintable.
Until that point it all works as expected, but as soon as I started to import the data
into the maintable (from the temptable) I ran into an error with the datetime field in the maintable.
I always get an error message there.
SQL
The fields in mytemptable are (Nr - int and datum - varchar(50)) the fields in mymaintable (orderNo - int and orderDate - Datetime).
INSERT INTO mymaintable (orderNo, orderDate) SELECT
Nr,
TRY_PARSE(CASE WHEN len(datum) = 9
THEN left(Datum,6) + CAST((CAST(right(Datum, 3) AS int) + 1900) AS varchar(4)) + ' 00:00:00.000'
ELSE datum + ' 00:00:00.000'
END AS DATETIME USING 'de-DE') AS datum
FROM mytemptable;
Error message
Msg 6521, Level 16, State 1, Line 1 A .NET Framework error occurred
during statement execution: System.Data.SqlTypes.SqlTypeException:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and
12/31/9999 11:59:59 PM. System.Data.SqlTypes.SqlTypeException: at
System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value) at
System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value) at
System.Data.SqlServer.Internal.CXVariantBase.DateTimeToSSDate(DateTime
dt)
Additional infos
I tried only the select part with the same result, but another set of data (only 1k out of the 20k rows) functions without raising the error.
Edit:
I tried to solve the problem with what was suggested in another post to use datetime2 instead of datetime and that functioned. BUT when I tried to find the rows that way by getting those where the datetime2 conversion returned null I received no result. Which means in essence DAtetime conversion fails, but datetime2 conversion does not fail at all.
Question
From what I saw (as I used it on a different part of the data already successfully) it seems not to be a general error, but one data related.
So my question is there:
Is there any way of finding out which exactlyrow(s) is causing the problems?
(as I understood try_parse it should return null when a conversion error happens and NOT throw an exception)
Thanks