Introduction
SQL SERVER 2005 discovered a new concept TRY-CATCH to handle unexpected behavior in SQL statements. We can use TRY-CATCH inside Stored Procedure, Triggers to handle exception and raise error message. Often we need to convert one data type value to other data type. For instance, there is a string(varchar) type variable and we need to convert its date or integer type - in case string value is blank and you are trying to convert to date type then SQL SERVER will throw you exception due to type casting. In order to avoid conversion problem SQL SERVER 2012 introduced the following three functions which helps us to handle type conversion exception. These functions are:
- TRY_PARSE
- TRY_CONVERT
- TRY_CAST
Let’s discuss how it can be implemented:
TRY_PARSE
It converts string data type to target data type(Date or Numeric). For example, source data is string type and we need to convert to date type. If conversion attempt fails it returns NULL value.
Syntax: TRY_PARSE (string_value AS data_type [ USING culture ])
- String_value – This is argument is source value which is NVARCHAR(4000) type.
- Data_type – This argument is target data type either date or numeric.
- Culture – It is an optional argument which helps to convert the value to in Culture format. Suppose you want to display the date in French, then you need to pass culture type as ‘Fr-FR’. If you will not pass any valid culture name, then PARSE will raise an error.
Examples
- DECLARE @ fakeDate AS varchar(10);
- DECLARE @ realDate AS VARCHAR(10);
- SET @fakeDate = 'iamnotadate';
- SET @realDate = '13/09/2015;
- SELECT TRY_PARSE(@fakeDate AS DATE); --NULL
- SELECT TRY_PARSE(@realDate AS DATE); -- 2015-09-13
- SELECT TRY_PARSE(@realDate AS DATE USING 'Fr-FR'); -- 2015-09-13
TRY_CONVERT
It converts value to specified data type and if conversion fails it returns NULL. For example, source value in string format and we need date/integer format. Then this will help us to achieve the same.
Syntax: TRY_CONVERT ( data_type [ ( length ) ], expression [, style ] )
- Data_type - The datatype into which to convert. Here length is an optional parameter which helps to get result in specified length.
- Expression - The value to be converted
- Style - It is an optional parameter which determines formatting. Suppose you want date format like “May, 18 2013” then you need pass style as 111. More on style visit here.
Examples:

Kiran GottumukkalaPosted Jun 18, 2019, 5:11 AM
Good article. But Try_Convert works on Azure Sql as well. We are using, and it never thrown any sort of error.
Santhakumar MunuswamyPosted Sep 17, 2015, 10:33 AM
Thanks for nice article:)
Gowtham RajamanickamPosted Sep 15, 2015, 7:56 AM
Good one..
Sibeesh VenuPosted Sep 15, 2015, 4:46 AM
Nice Share
Humayun Kabir MamunPosted Sep 15, 2015, 2:46 AM
Nice...
Jaipal ReddyPosted Sep 15, 2015, 12:17 AM
Nice one sir
Harshad PansuriyaPosted Sep 15, 2015, 12:13 AM
Nice One Sir
Karthikeyan KPosted Sep 14, 2015, 11:24 PM
Good one sir...Thanks for sharing
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 2:17 PM
Nice Explain Manas.......
Shridhar SharmaPosted Sep 14, 2015, 2:14 PM
good one.
Mohammed IbrahimPosted Sep 14, 2015, 1:05 PM
nice information