Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TRY_PARSE vs TRY_CONVERT

Tags:

sql

sql-server

I am currently studying for my MCSA Data Platform, I got the following question wrong and I was looking for an explanation as to why my answer was wrong as the in test explanation did not make much sense.

You have a table named AuditTrail that tracks modifications to data in other tables. The AuditTrail table is updated by many processes. Data input into AuditTrail may contain improperly formatted datetime values. You implement a process that retrieves data from various columns, but sometimes the process throws error when its unable to convert the data into valid date time values. You need to convert the data into a valid date time value using the en-US format culture code. If the conversion fails, a null value must be returned in the en-US format culture code. If the conversion fails, a null value must be returned in the column output and the conversion process must not throw error.

The options were TRY_PARSE function vs TRY_CONVERT function.

I chose TRY_PARSE but the correct answer is TRY_CONVERT.

Can anybody clarify why TRY_PARSE is not a valid choice in this example as I do not want to make this mistake in the future

like image 926
Aura Avatar asked Apr 16 '18 23:04

Aura


People also ask

What is Try_convert in SQL?

TRY_CONVERT takes the value passed to it and tries to convert it to the specified data_type. If the cast succeeds, TRY_CONVERT returns the value as the specified data_type; if an error occurs, null is returned. However if you request a conversion that is explicitly not permitted, then TRY_CONVERT fails with an error.

What is the difference between Try_convert and convert?

If the cast fails, the TRY_CONVERT() function returns NULL while the CONVERT() function raises an error. This is the main difference between the two functions. You can use the NULL handling functions or expressions such as ISNULL() and COALESCE to handle the result of the TRY_CONVERT() function in case the cast fails.

What is Try_parse in SQL?

Returns the result of an expression, translated to the requested data type, or null if the cast fails in SQL Server. Use TRY_PARSE only for converting from string to date/time and number types.

What is the difference between cast and Try_cast in SQL Server?

A special version of CAST , :: that is available for a subset of data type conversions. It performs the same operation (i.e. converts a value of one data type into another data type), but returns a NULL value instead of raising an error when the conversion can not be performed.


2 Answers

One of the main differences between TRY_PARSE and TRY_CONVERT is that TRY_PARSE accepts a culture with a USING clause. Though poorly worded and wrapped in assumptions, this question seems to be suggesting that the input & system cultures are the same.

So in this case TRY_CONVERT would be the simpler of the two calls, since the default culture is assumed to be 'en-US'.

Had you needed to parse a french date from a string, the choice would have to be TRY_PARSE, since it allows you to specify the culture.

TRY_PARSE ('mars 18 2018' AS DATETIME USING 'fr-FR')  

Source: https://docs.microsoft.com/en-us/sql/t-sql/functions/try-parse-transact-sql?view=sql-server-2017

like image 141
dperish Avatar answered Oct 07 '22 06:10

dperish


  1. TRY_PARSE can only be used for converting from string to date/time or number data types where as TRY_CONVERT can be used for any general type conversions. For example, you can use TRY_CONVERT to convert a string to XML data type, where as you can do the same using TRY_PARSE

  2. Converting a string to XML data type using TRY_CONVERT

    SELECT TRY_CONVERT(XML, '') AS [XML]

The above query produces the following

XML     <root></child></root>
  1. Converting a string to XML data type using TRY_PARSE

    SELECT TRY_PARSE('' AS XML) AS [XML]

The above query will result in the following error
  Invalid data type xml in function TRY_PARSE
  • Another difference is TRY_PARSE relies on the presence of .the .NET Framework Common Language Runtime (CLR) where as TRY_CONVERT does not.
like image 38
Zoyeb Shaikh Avatar answered Oct 07 '22 08:10

Zoyeb Shaikh