Labels

Wednesday, April 3, 2013

TRY_CONVERT

-- Often in queries, we can see conversion errors like "conversion failed when converting from .... "
--If we wants to hide these errors and instead of exception, if we want a NULL value, we can use this TRY_CONVERT() function.

SET DATEFORMAT mdy;
SELECT TRY_CONVERT(datetime2, '12/31/2010') AS Result;
GO

SET DATEFORMAT dmy;
SELECT TRY_CONVERT(datetime2, '12/31/2010') AS Result;
GO

No comments:

Post a Comment