Latest Post

Monday, 24 October 2016

How to Use TRY_CONVERT SQL Function

TRY_CONVERT() Function

The TRY_CONVERT works like the CONVERT function. TRY_CONVERT function returns a NULL instead of generating an error when the input value isn’t convertible. Here’s an example, followed by its output:

Syntax:- TRY_CONVERT (data_type ,input_value )

Example:-
DECLARE @InputString1 AS varchar(10);  
DECLARE @InputString2 AS VARCHAR(10);  
SET @InputString1 = 'www.code-view.com';
SET @InputString2 = '08/14/2016'; 

SELECT TRY_CONVERT(INT, @InputString1); -Output- NULL  
SELECT TRY_CONVERT(DATETIME, @InputString1); -Output- NULL  
SELECT TRY_CONVERT(DATETIME, @InputString2, 111); -Output- Sep, 13 2015