In SQL Server, the TRY_PARSE()
function returns the result of an expression, translated to the requested data type, or NULL
if the conversion fails.
Basically, it works the same as the PARSE()
function, except that it returns NULL
instead of an error if the cast fails.
Both functions are intended for converting string values to either date/time or number types.
Syntax
The syntax goes like this:
TRY_PARSE ( string_value AS data_type [ USING culture ] )
The optional culture argument allows us to specify the culture in which string_value is formatted. If we don’t provide this argument, the language of the current session is used.
Example
Here’s an example of using TRY_PARSE()
to parse a string as a date:
SELECT TRY_PARSE( 'Sunday, 22 January 2023' AS date);
Result:
2023-01-22
Here’s what happens if the cast fails:
SELECT TRY_PARSE( 'Friday, 22 January 2023' AS date);
Result:
(null)
Here, I changed Sunday
to Friday
, but I left the remaining part of the date the same. Therefore, the value I provided wasn’t a valid date and it couldn’t be converted to a valid date type, and so we got NULL
.
Here’s what happens if we use PARSE()
instead of TRY_PARSE()
:
SELECT PARSE( 'Friday, 22 January 2023' AS date);
Result:
Error converting string value 'Friday, 22 January 2023' into data type date using culture ''.
Using the PARSE()
function in this case results in an error, whereas TRY_PARSE()
simply returned NULL
.
Parse to Money
Here’s one that parses a string as money:
SELECT TRY_PARSE( '$300' AS money);
Result:
300
We can use the sys.dm_exec_describe_first_result_set
function to check the data type of the result:
SELECT system_type_name
FROM sys.dm_exec_describe_first_result_set(
'SELECT TRY_PARSE( ''$200'' AS money)',
null,
0);
Result:
money
The culture
Argument
We can use the culture
argument to specify how the string value that we provide is formatted. This can be handy when passing a date that’s in a format specific to a certain locale.
Example:
SELECT TRY_PARSE( '23/01/2023' AS date USING 'en-GB');
Result:
2023-01-23
In this case, the string I provided represents a date in the British format (en-GB
), and so the TRY_PARSE()
function was able to work out what date it is.
Here’s what happens when I change the culture
argument to en-US
:
SELECT PARSE( '23/01/2023' AS date USING 'en-US');
Result:
Error converting string value '23/01/2023' into data type date using culture 'en-US'.
The error occurred because this culture assumes that the date is provided in mm/dd/yyyy format (because I specified the en-US
culture), but months don’t go past 12. Therefore, the only thing it could do was throw an error, because it can’t create a date with the 20th month.
More Info
Also see PARSE()
vs TRY_PARSE()
in SQL Server: What’s the Difference?.
See PARSE()
vs CAST()
vs CONVERT()
in SQL Server: What’s the Difference? if you’re trying to figure out which function to use. The TRY_
versions of each function work the same way, except that they return NULL
instead of an error whenever a cast fails.
And check out the Microsoft documentation for more information, including a list of locales that can be used with TRY_PARSE()
, as well as the valid data types that we can convert to.