Use try_cast:
... then try_cast(v.value as datetime)
else try_cast(v.value as datetime)
endJust don't mess a datetime value as a string in the first place if you can. Any invalid datetime string will become null.
I HIGHLY recommend NOT to use TRY_CAST for the same reason I recommend NOT to use CAST.
Instead we should use TRY_CONVERT and always use the style explicitly, or use CONVERT instead of CAST and use the style explicitly
* I said a long long long time ago that Microsoft should remove the option of CAST without the style option, and let us use only CONVERT. The old CONNECT site was removed. Maybe I need to open a new CONNECT in the new feedback website regarding this....
Image may be NSFW. Clik here to view. ![]() | Ronen Ariely [Personal Site] [Blog] [Facebook] [Linkedin] |