Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server TRY_PARSE: Safe Conversion Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server's TRY_PARSE function converts a string to a date, time, or number using .NET's parsing rules, and returns NULL instead of an error when the conversion fails. That makes it the safe choice for cleaning imported or user-entered data: you can test whether a value is convertible before you rely on it, rather than having a whole query abort on one bad row. It requires SQL Server 2012 or later and a database compatibility level of 110 or higher.

What You'll Need

  • SQL Server 2012 or newer (any edition), or Azure SQL Database.
  • A database with compatibility level 110 or higher. Check with SELECT compatibility_level FROM sys.databases WHERE name = 'YourDb'; and raise it with ALTER DATABASE YourDb SET COMPATIBILITY_LEVEL = 110; if needed.
  • Permission to run SELECT queries against the table you are testing.
  • A SQL client such as SQL Server Management Studio (SSMS) or Azure Data Studio.

Step-by-Step

  1. Understand the syntax. The form is TRY_PARSE ( string_value AS data_type [ USING culture ] ). The first argument must be a string (or something implicitly convertible to nvarchar). The second is the target type, and the optional third is a culture name such as 'en-US' or 'de-DE'.
  2. Pick the right target type. TRY_PARSE only supports date, time, datetime, datetime2, datetimeoffset, and the numeric types (tinyint, smallint, int, bigint, decimal, numeric, money, float, real). It does not convert to varchar, char, or bit. If you need those, use TRY_CONVERT or TRY_CAST instead.
  3. Run a simple test. In a query window, execute:
    SELECT TRY_PARSE('2024-03-15' AS date) AS ParsedDate;
    You should get 2024-03-15 back. Now try SELECT TRY_PARSE('not a date' AS date) AS ParsedDate; and you will get NULL rather than an error.
  4. Test the culture argument. Run SELECT TRY_PARSE('15/03/2024' AS date USING 'en-GB') AS UKDate; and then the same string with USING 'en-US'. The first returns 15 March; the second returns NULL because en-US expects month first. This is the main reason to use TRY_PARSE over TRY_CONVERT: it respects the culture you name.
  5. Apply it to a column. Suppose you have a staging table dbo.Import with an nvarchar column OrderDate. Write:
    SELECT OrderID, TRY_PARSE(OrderDate AS date USING 'en-GB') AS CleanDate FROM dbo.Import;
    Rows that cannot be parsed come back as NULL, so you can inspect them separately.
  6. Find the rows that fail. Wrap the call in a filter:
    SELECT * FROM dbo.Import WHERE TRY_PARSE(OrderDate AS date USING 'en-GB') IS NULL AND OrderDate IS NOT NULL;
    That gives you the problem records to fix before loading.
  7. Load only the good rows. Use the same expression in an INSERT ... SELECT, or in a CASE expression to substitute a default date. Because TRY_PARSE returns NULL rather than raising an error, one bad row will not stop the batch.
  8. Check performance on large tables. TRY_PARSE calls into the .NET CLR, so it is slower than TRY_CONVERT or TRY_CAST for the same job. If you are converting millions of rows and do not need culture handling, prefer TRY_CONVERT. If you do need TRY_PARSE, avoid applying it inside a WHERE clause on a large table where possible; instead clean the data once into a typed column.

Common Mistakes

  • Passing a non-string first argument. TRY_PARSE expects text. Passing a date or numeric column directly can work through implicit conversion but often produces surprising results; convert to nvarchar explicitly if in doubt.
  • Assuming the server's default culture. Without a USING clause, TRY_PARSE uses the session language. A query that works on one server can return NULL on another with a different default language. Always specify the culture when the input format is ambiguous.
  • Using it for varchar targets. TRY_PARSE cannot return a string type. Use TRY_CONVERT or TRY_CAST for that.
  • Confusing NULL with zero or an empty string. A failed parse returns NULL, not 0. If downstream code treats NULL as a valid value, you will silently load bad data.
  • Forgetting the compatibility level. On a database set below 110, TRY_PARSE raises an error about an unrecognized function name. Raise the compatibility level or use TRY_CONVERT.
  • Expecting it to trim or clean input. TRY_PARSE does not remove currency symbols, spaces, or thousands separators unless the culture rules allow them. Clean the string first with LTRIM/RTRIM or REPLACE.

Tips

  • Use TRY_PARSE when the input format varies by region or when the string comes from a file with a known locale.
  • Use TRY_CONVERT when you have a fixed format such as ISO 8601 (yyyy-mm-dd) and want the fastest conversion.
  • Pair TRY_PARSE with ISNULL or COALESCE to supply a fallback value in reports.
  • Log the raw string alongside the parsed result so you can trace failures later.
  • Test with both valid and deliberately invalid samples before running against production data.

Troubleshooting

Error: 'TRY_PARSE' is not a recognized built-in function name. The database compatibility level is below 110, or you are on SQL Server 2008 or earlier. Check the level as shown above and raise it, or switch to TRY_CONVERT.

Everything returns NULL. The culture is wrong for the input format. Try the matching culture (for example 'en-GB' for day-first dates) or check for leading/trailing spaces with LEN and DATALENGTH.

Some rows parse, others do not. The source data mixes formats. Group by format or clean the column before parsing; do not assume a single culture will handle every row.

Query is slow. TRY_PARSE is slower than TRY_CONVERT because of the CLR call. Move the conversion into a staging step, or use TRY_CONVERT where culture handling is not required.

Unexpected numeric results. Numeric parsing follows .NET rules, so a string like '1,234.56' may fail or succeed depending on culture. Specify the culture explicitly and test with representative values.

Frequently Asked Questions

What is the difference between TRY_PARSE and PARSE?

PARSE raises an error when conversion fails; TRY_PARSE returns NULL instead. Both use .NET culture-aware parsing.

Does TRY_PARSE work on SQL Server 2008?

No. It was introduced in SQL Server 2012 and requires compatibility level 110 or higher.

Can TRY_PARSE convert to varchar?

No. It supports date/time and numeric target types only. Use TRY_CONVERT or TRY_CAST for strings.

Why does TRY_PARSE return NULL for a date that looks valid?

Almost always a culture mismatch. The session language or the USING culture expects a different order of day, month, and year.

Is TRY_PARSE faster than TRY_CONVERT?

No, it is generally slower because it calls into the CLR. Use TRY_CONVERT when you do not need culture-aware parsing.

Can I use TRY_PARSE in a WHERE clause?

Yes, but it runs per row and can be slow on large tables. Filter on a cleaned column or a staging table where possible.