T-SQL Tutorial

T-SQL Try_Cast


The Try_Cast function returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.

Try_Cast Syntax


TRY_CAST ( expression AS data_type [ ( length ) ] )



Try_Cast Example


SELECT TRY_CAST('08/17/2019' AS date);
SELECT TRY_CAST('08/17/2019 09:54:45' AS date) AS Result;

SELECT TRY_CAST('08/17/2019 09:54:45' AS datetime) AS Result;
SELECT TRY_CAST('08/17/2019 09:54:45' AS datetime2) AS Result;

See also:
T-SQL Functions
Cast -> Convert -> Parse -> Try_Convert -> Try_Parse