TRY_PARSE Function - SQL


Overview


The TRY_PARSE function in SQL attempts to convert a string representation of a date and/or time to a datetime or datetime2 data type. If the conversion is successful, it returns the converted value; otherwise, it returns NULL.

Example:

SELECT TRY_PARSE(('2024-01-01 12:34:56' AS datetime2))

Syntax:

SELECT TRY_PARSE(string_value AS data_type [ USING culture ])

string_value is the string representation of the date and/or time that you want to convert into a datetime or datetime2 data type.

data_type specifies the target data type (datetime or datetime2) to which the string should be parsed.

culture (optional) specifies the culture whose format will be used for parsing. If not specified, the current session's language setting is used for parsing the date and time string.