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
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.
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.
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.
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.
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
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
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With