{"id":2407,"date":"2018-06-07T21:58:04","date_gmt":"2018-06-08T04:58:04","guid":{"rendered":"https:\/\/database.guide\/?p=2407"},"modified":"2023-01-21T16:02:21","modified_gmt":"2023-01-21T06:02:21","slug":"parse-vs-try_parse-sql-server-whats-the-difference","status":"publish","type":"post","link":"https:\/\/database.guide\/parse-vs-try_parse-sql-server-whats-the-difference\/","title":{"rendered":"PARSE() vs TRY_PARSE() in SQL Server: What&#8217;s the Difference?"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-sql-server\/\">SQL Server<\/a>, the <code><a href=\"https:\/\/database.guide\/how-parse-works-in-sql-server\/\">PARSE()<\/a><\/code> and <code><a href=\"https:\/\/database.guide\/how-try_parse-works-in-sql-server\/\">TRY_PARSE()<\/a><\/code> functions are used for translating a value into another data type. They essentially do the same thing, with one exception; how they deal with errors.<\/p>\n<p>If <code>PARSE()<\/code> fails when attempting to parsing to a different data type, it will return an error. If <code>TRY_PARSE()<\/code> fails, it will return <code>NULL<\/code>.<\/p>\n<p><!--more--><\/p>\n<h2>Example 1 &#8211; First, the Similarities<\/h2>\n<p>Here&#8217;s an example that demonstrates how both functions return the same result when they can successfully parse the value to the required data type:<\/p>\n<pre>SELECT \n    PARSE('Fri, 8 June 2018' AS date) AS PARSE,\n    PARSE('Fri, 8 June 2018' AS date) AS TRY_PARSE;<\/pre>\n<p>Result:<\/p>\n<pre>+------------+-------------+\n| PARSE      | TRY_PARSE   |\n|------------+-------------|\n| 2018-06-08 | 2018-06-08  |\n+------------+-------------+\n<\/pre>\n<p>As expected, they both return exactly the same result.<\/p>\n<p>But let&#8217;s see what happens when they are unable to parse the value to the required data type.<\/p>\n<h2>Example 2 &#8211; When PARSE() Fails<\/h2>\n<p>Here&#8217;s an example of what happens when\u00a0<code>PARSE()<\/code> is unable to parse a value to another value:<\/p>\n<pre>SELECT PARSE('Next year' AS date) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>Error converting string value 'Next year' into data type date using culture ''. \n<\/pre>\n<p>The operation fails because I didn&#8217;t provide a valid representation of the requested data type. In other words, <code>PARSE()<\/code> can&#8217;t convert <code>Next year<\/code> into a <strong>date<\/strong> data type as requested.<\/p>\n<h2>Example 3 &#8211; When TRY_PARSE() Fails<\/h2>\n<p>Here&#8217;s an example when we try to parse the same value with\u00a0<code>TRY_PARSE()<\/code>:<\/p>\n<pre>SELECT TRY_PARSE('Next year' AS date) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\n| Result   |\n|----------|\n| NULL     |\n+----------+\n<\/pre>\n<p>The parse still fails, but it returns <code>NULL<\/code> instead of an error.<\/p>\n<h2>Example 4 &#8211; Using TRY_PARSE() with a Conditional Statement<\/h2>\n<p>We can take <code>TRY_PARSE()<\/code> and test its return value. If it&#8217;s a NULL value,\u00a0we can return one thing, if it&#8217;s a non-NULL value, we can return another:<\/p>\n<pre>SELECT \n    CASE WHEN TRY_PARSE('Next year' AS date) IS NULL\n        THEN 'Conversion failed'\n        ELSE 'Conversion succeeded'\n    END\nAS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+-------------------+\n| Result            |\n|-------------------|\n| Conversion failed |\n+-------------------+\n<\/pre>\n<h2>Example 5 &#8211; TRY_PARSE() With Error<\/h2>\n<p>Just because <code>TRY_PARSE()<\/code> doesn&#8217;t result in an error in the above examples, it doesn&#8217;t mean that it <em>never<\/em> results in an error. There are times where you can still get an error while using this function.<\/p>\n<p>For example, you&#8217;ll get an error if you provide an invalid value as the <code>culture<\/code> argument:<\/p>\n<pre>SELECT TRY_PARSE('Next year' AS date USING 'Mars') AS Result;\n<\/pre>\n<p>Result:<\/p>\n<pre>The culture parameter 'Mars' provided in the function call is not supported. \n<\/pre>\n<h2>Some Notes about these Functions<\/h2>\n<p>Here are some points that Microsoft has to <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/parse-transact-sql?view=sql-server-2017#remarks\">say<\/a> about these functions:<\/p>\n<ul>\n<li>It&#8217;s recommended that you use <code>PARSE()<\/code> and\u00a0<code>TRY_PARSE()<\/code> only for converting from string to date\/time and number types. For other data types, use <a href=\"https:\/\/database.guide\/how-cast-works-in-sql-server\/\"><code>CAST()<\/code><\/a> or <a href=\"https:\/\/database.guide\/convert-in-sql-server\/\"><code>CONVERT()<\/code><\/a>.<\/li>\n<li>These functions rely on the presence of .the .NET Framework Common Language Runtime (CLR).<\/li>\n<li>There&#8217;s a certain performance overhead in parsing the string value.<\/li>\n<li>These functions will not be remoted since they depend on the presence of the CLR. Trying to remote a function that requires the CLR would cause an error on the remote server.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In SQL Server, the PARSE() and TRY_PARSE() functions are used for translating a value into another data type. They essentially do the same thing, with one exception; how they deal with errors. If PARSE() fails when attempting to parsing to a different data type, it will return an error. If TRY_PARSE() fails, it will return &#8230; <a title=\"PARSE() vs TRY_PARSE() in SQL Server: What&#8217;s the Difference?\" class=\"read-more\" href=\"https:\/\/database.guide\/parse-vs-try_parse-sql-server-whats-the-difference\/\" aria-label=\"Read more about PARSE() vs TRY_PARSE() in SQL Server: What&#8217;s the Difference?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5],"tags":[72,116,93,77,61],"class_list":["post-2407","post","type-post","status-publish","format-standard","hentry","category-sql","category-sql-server","tag-conversion-functions","tag-convert","tag-functions","tag-mssql","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=2407"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2407\/revisions"}],"predecessor-version":[{"id":28833,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2407\/revisions\/28833"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}