Quantcast
Viewing latest article 7
Browse Latest Browse All 16

Conversion failed when converting date and/or time from character string

Use try_cast:

... then try_cast(v.value as datetime)
else  try_cast(v.value as datetime)
end

Just 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.
signature
  Ronen Ariely
 [Personal Site]    [Blog]    [Facebook]   [Linkedin]


Viewing latest article 7
Browse Latest Browse All 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>