I have a situation where I need to convert datetime string values in a column to a more standardised format, the converting process is quiet manual I am not just casting and storing.
So the script grabs the value, fumbles it around and stores it back in the same cell in a ODBC format. The thing is that I need to be able to check whether the value stored in the cell has already been converted or still needs to be processed as the script needs to be re-runnable.
So the example situation that I have is I come across the following two values:
08/03/2017 3:51 PM
28/03/2017 7:30 AM
Now I know for a fact that it is day first.
If I use
TRY_CONVERT(DATETIME, [Value], 121)
I will get the resulting value:
2017-08-03 15:51:00.000
NULL
Now I would like to get is NULL for both, so it is correct for the second value but not the first and worse it swapped the day and month components for the first one.
However if I use
TRY_CONVERT(DATETIME2, [Value], 121)
I do get NULL for both note, so my question is what is the difference in converting with DATETIME and DATETIME2? Why does DATETIME2 return NULL for the first?
I know that it is returning NULL for the second value because the first componentt is greater than 12.
Side note:
At the end of my script I end up with the ODBC form for the values (probably should be ISO?)
2016-06-07 03:23:17.000
2016-06-07 03:23:17.000
When I run
TRY_CONVERT(DATETIME2, [Value], 121)
I happily get not NULL values so I don't have to covert it :)