SQL TRY_PARSE Function

The SQL TRY_PARSE is one of the SQL Conversions Function. This SQL TRY_PARSE function converts the String data to the requested data type and returns the result as an expression. I suggest you to use this SQL TRY_PARSE function to convert the string data to either Date/time, or Number type.

SQL TRY_PARSE Function Syntax

If the SQL Try_Parse function is unable to convert the string into the desired data type, or if we pass the string as non-convertible, then this function will return NULL. The syntax of the SQL Server TRY_PARSE Function is

TRY_PARSE (String_Value AS Data_Type [USING Culture])

-- For example
SELECT TRY_PARSE (String_Column AS Data_Type USING 'en-US') AS [Column1]
FROM [Source]
  • String_Value: Please specify the valid string that you want to convert as the desired data type.
  • Data_Type: Here you have to specify the Data Type to which you want to convert the String_Value
  • Culture: This is an optional parameter. And if you ignore the parameter, then the function will use the language of the current session.

SQL TRY_PARSE Function Example 1

The SQL Server Try_Parse Function is mainly used to convert the string values into date and time, and numeric values. The following query will parse the string to integer and decimal. And also parse the string to date time.

-- SQL TRY_PARSE FUNCTION Example 
DECLARE @str AS VARCHAR(50)
SET @str = '1234'

SELECT TRY_PARSE(@str AS INT) AS Result; 

-- Direct Inputs
SELECT TRY_PARSE('4455' AS DECIMAL(10, 2)) AS Result; 
 
SELECT TRY_PARSE('03/03/2017' AS DATETIME) AS Result;  

SELECT TRY_PARSE('03/03/2017' AS DATETIME2) AS Result; 

SELECT TRY_PARSE('Tutorial Gateway' AS DATETIME USING 'en-US') AS Result;
SQL TRY_PARSE Function 1

Below lines of SQL Server code are used to declare a variable of VARCHAR Data Type. Next, we were assigning the string data ‘1234’.

DECLARE @str AS VARCHAR(50)
SET @str = '1234'

From the below statement, you can see that we are using try parse function for converting the string to an integer. We also assigned new names to the result as ‘Result’ using the ALIAS Column.

SELECT TRY_PARSE(@str AS INT) AS Result;

In the next line, We used the SQL TRY_PARSE function directly on string and converting it to a decimal value

SELECT TRY_PARSE('4455' AS DECIMAL(10, 2)) AS Result;

Next, we are converting the string to Date-Time and DateTime 2 data type. Please refer to SQL Data Types to understand the datatype limitations.

SELECT TRY_PARSE('03/03/2017' AS DATETIME) AS Result;  

SELECT TRY_PARSE('03/03/2017' AS DATETIME2) AS Result;

Lastly, we tried to convert the ‘Tutorial Gateway’ string to date time. As you can see, it is not possible so, this SQL Server Try Parse function is returning NULL as output.

SELECT TRY_PARSE('Tutorial Gateway' AS DATETIME USING 'en-US') AS Result;

Use IIF with SQL TRY_PARSE

In this example, we show you how to use IIF Statement to handle the NULLs returned by the TRY_PARSE. It can be helpful when you are inserting records, and you don’t know whether the column has date and time or not. In this case, you can use this function to check, and if not, we can replace it with the current date and time.

SELECT IIF(
	    TRY_PARSE('Tutorial Gateway' AS DATETIME) IS NULL, 
		       GETDATE(), 
		       'False'
	   ) AS Result;

Here we used the IIF statement along with the IS NULL statement to check whether the result expression is null or not.

SQL TRY_PARSE Function 2