How to Use TRY_PARSE() SQL function

TRY_PARSE() function

TRY_PARSE() function is introduced in SQL Server 2012. it convert any string value to Date/Time format or Numeric . If the input string value cannot be converted to Numeric or Date/Time format, it will give NULL result.

Syntax:- TRY_PARSE (input_value  AS data_type [ USING culture ])

DECLARE @InputString1 AS varchar(10);  
DECLARE @InputString2 AS VARCHAR(10);  
SET @InputString1 = '';
SET @InputString2 = '08/14/2016'; 

SELECT TRY_PARSE(@InputString2 AS DATE); -- 2016-08-14  
SELECT TRY_PARSE(@InputString2 AS DATE USING 'en-UK'); -- 2016-08-14 

First query try to parse a string to date type, it fails and returns NULL value.
Second query successfully parse to date type and return result -- 2016-08-14.
Third query also successfully parse with in UK format.
How to Use TRY_PARSE() SQL function How to Use TRY_PARSE() SQL function Reviewed by Alok Singh on 02:03 Rating: 5
Powered by Blogger.