{"id":968,"date":"2013-06-08T21:34:45","date_gmt":"2013-06-08T16:04:45","guid":{"rendered":"http:\/\/sqlhints.com\/?p=968"},"modified":"2013-07-07T03:07:18","modified_gmt":"2013-07-06T21:37:18","slug":"parse-sql-server-2012-built-in-conversion-function","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2013\/06\/08\/parse-sql-server-2012-built-in-conversion-function\/","title":{"rendered":"PARSE CONVERSION FUNCTION IN SQL SERVER 2012"},"content":{"rendered":"<p><strong>PARSE <\/strong>is one of the new built-in conversion function introduced as a Part of Sql Server 2012. PARSE function converts the string expression to the requested data type. It tries it&#8217;s best to translate the string\u00a0value to requested type but if it fails to\u00a0translate then raises an exception.<\/p>\n<p><strong>Important Note:<\/strong> This PARSE function is not a native Sql function, instead it is a .NET Framework Common Language Run-time dependent function. Then obviously it will have the performance overhead and also requires the presence of .NET CLR on the database Server. Continue to use the existing CAST and CONVERT functions wherever it is possible.<\/p>\n<p>Syntax:\u00a0 PARSE ( string_value AS data_type [ USING culture ] )<br \/>\n<strong><br \/>\nParameter Details:<\/strong><br \/>\n<strong>string_value\u00a0<\/strong>\u00a0\u00a0: String\u00a0expression which needs to be parsed.<br \/>\n<strong>data_type<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0: Output data type, e.g. INT, NUMERIC, DATETIME etc.<br \/>\n<strong>culture<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0: Optional string that identifies the culture in which string_value is formatted. If it is not specified, then it takes the language of the current session.<\/p>\n<p><strong>Now let us understand this\u00a0PARSE function with examples:<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true\">-- PARSE String to INT\r\nSELECT PARSE(&#039;1000&#039; AS INT) AS &#039;String to INT&#039;\r\n-- PARSE String to Numeric\r\nSELECT PARSE(&#039;1000.06&#039; AS NUMERIC(8,2)) AS &#039;String to Numeric&#039;\r\n-- PARSE String to DateTime\r\nSELECT PARSE(&#039;05-18-2013&#039; as DATETIME) AS &#039;String to DATETIME&#039;\r\n-- PARSE String to DateTime\r\nSELECT PARSE(&#039;2013\/05\/18&#039; as DATETIME) AS &#039;String to DATETIME&#039;\r\n-- PARSE string value in the India date format to DateTime \r\nSELECT PARSE(&#039;18-05-2013&#039; as DATETIME using &#039;en-in&#039;) \r\n AS &#039;String in Indian DateTime Format to DATETIME&#039;\r\n-- PARSE string value is in the US currency format to Money \r\nSELECT PARSE(&#039;$2500&#039; as MONEY using &#039;en-US&#039;) \r\n AS &#039;String in US Currency Format to MONEY&#039;<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/PARSE_SQL_SERVER_2012_CONVERSION_FUNCTION.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-980\" alt=\"PARSE_SQL_SERVER_2012_CONVERSION_FUNCTION\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/PARSE_SQL_SERVER_2012_CONVERSION_FUNCTION.jpg\" width=\"577\" height=\"545\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/PARSE_SQL_SERVER_2012_CONVERSION_FUNCTION.jpg 577w, https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/PARSE_SQL_SERVER_2012_CONVERSION_FUNCTION-300x283.jpg 300w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/a><\/p>\n<p><strong>Below example demonstrates how PARSE function tries it&#8217;s best convert the input string value to\u00a0 a specified data type even when the specified value is not in the correct format.<\/strong><\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">SELECT PARSE(&#039;08-JUNE-2013&#039; AS DATETIME)\r\nSELECT PARSE(&#039;08-JUN-2013&#039; AS DATETIME)\r\nSELECT PARSE(&#039;2013JUNE08&#039; AS DATETIME)\r\nSELECT PARSE(&#039;08\/JUN\/2013&#039; AS DATETIME)<\/pre>\n<p>All the above statements will return the same result and it is:\u00a0<em id=\"__mceDel\">2013-06-08 00:00:00.000<\/em><\/p>\n<p><strong>Now try to PARSE an invalid string value to DATETIME:<\/strong><\/p>\n<p>This conversion fails as February month will not have 31st day.<\/p>\n<pre class=\"brush: sql; gutter: true\">--PARSE invalid String to DATETIME\r\nSELECT PARSE(&#039;2012\/02\/31&#039; as DATETIME)\r\nGO<\/pre>\n<p>Result:<br \/>\n<span style=\"color: red;\">Msg 9819, Level 16, State 1, Line 1<br \/>\nError converting string value &#8216;2012\/02\/31&#8217; into data type datetime using culture &#8221;.<\/span><\/p>\n<p>Below PARSE function will fail to translate the date. Because the culture parameter specified is en-us, but the string value specified is not in US format. US Date format excepts month first instead of day and then day and year.<\/p>\n<pre class=\"brush: sql; gutter: true\">--PARSE invalid String to DateTime\r\nSELECT PARSE(&#039;18-05-2013&#039; as DATETIME using &#039;en-us&#039;)\r\nGO<\/pre>\n<p>Result:<br \/>\n<span style=\"color: red;\">Msg 9819, Level 16, State 1, Line 1<br \/>\nError converting string value &#8217;18-05-2013&#8242; into data type datetime using culture &#8216;en-us&#8217;.<\/span><\/p>\n<p><strong>Difference between PARSE and CONVERT function.<\/strong><\/p>\n<p>Below example demonstrates the difference between PARSE and CONVERT function. PARSE function will successfully converts the string &#8216;Saturday, 08 June 2013&#8217; to date time, but the CONVERT function fails to convert the same value. That is PARSE function tries it&#8217;s best to convert the input string value to the requested type, but CONVERT function requires the input string to be exact format no variations allowed.<\/p>\n<pre class=\"brush: sql; gutter: true\">--PARSE Function Succeeds\r\nSELECT PARSE(&#039;Saturday, 08 June 2013&#039; AS DATETIME) \r\n                 AS &#039;PARSE Function Result&#039; GO\r\nSELECT PARSE(&#039;Sat, 08 June 2013&#039; AS DATETIME) \r\n                 AS &#039;PARSE Function Result&#039; GO\r\nSELECT PARSE(&#039;Saturday 08 June 2013&#039; AS DATETIME) \r\n                 AS &#039;PARSE Function Result&#039; GO\r\n\r\n--CONVERT Function Fails\r\nSELECT CONVERT(DATETIME, &#039;Saturday, 08 June 2013&#039;) \r\n                 AS &#039;CONVERT Function Result&#039; GO\r\nSELECT CONVERT(DATETIME, &#039;Sat, 08 June 2013&#039;) \r\n                 AS &#039;CONVERT Function Result&#039; GO\r\nSELECT CONVERT(DATETIME, &#039;Saturday 08 June 2013&#039;) \r\n                 AS &#039;CONVERT Function Result&#039; GO<\/pre>\n<p><strong>RESULT:<\/strong><\/p>\n<p>PARSE Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n2013-06-08 00:00:00.000<\/p>\n<p>PARSE Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n2013-06-08 00:00:00.000<\/p>\n<p>PARSE Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n2013-06-08 00:00:00.000<\/p>\n<p>CONVERT Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<span style=\"color: red;\">Msg 241, Level 16, State 1, Line 3<br \/>\nConversion failed when converting date and\/or time from character string.<\/span><\/p>\n<p>CONVERT Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<span style=\"color: red;\">Msg 241, Level 16, State 1, Line 1<br \/>\nConversion failed when converting date and\/or time from character string.<\/span><\/p>\n<p>CONVERT Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<span style=\"color: red;\">Msg 241, Level 16, State 1, Line 1<br \/>\nConversion failed when converting date and\/or time from character string.<\/span><\/p>\n<p>You may like to read the below new built-in function&#8217;s introduced in Sql Server 2012:<\/p>\n<table border=\"1\">\n<tr style=\"border: 1pt solid\">\n<td  style=\"border: 1pt solid windowtext; width: 50%; text-align: center;background-color:#F43722; color:white;\" valign=\"top\" Colspan =\"2\"><strong>New Built in Functions introduced in Sql Server<\/strong><\/td>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>CONVERSION FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/parse-sql-server-2012-built-in-conversion-function\/\" title=\"PARSE Conversion Function in Sql Server 2012\" target=\"_blank\">PARSE<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/try_parse-sql-server-2012-built-in-conversion-function\/\" title=\"TRY_PARSE Conversion Function in Sql Server 2012\" target=\"_blank\">TRY_PARSE<\/a> <\/strong><\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\" colspan=\"2\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/try_convert-sql-server-2012-built-in-conversion-function\/\" title=\"TRY_CONVERT Conversion Function in Sql Server 2012\" target=\"_blank\">TRY_CONVERT<\/a><\/strong><\/td>\n<tr >\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>STRING FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2012\/12\/02\/concat-function-in-sql-server-2012\/\" title=\"CONCAT String Function in Sql Server 2012\" target=\"_blank\">CONCAT<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/23\/format-string-function-in-sql-server-2012\/\" title=\"FORMAT String Function in Sql Server 2012\" target=\"_blank\">FORMAT<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>LOGICAL FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/15\/choose-logical-function-in-sql-server-2012\/\" title=\"CHOOSE Logical Function in Sql Server 2012\" target=\"_blank\">CHOOSE<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/15\/iif-logical-function-in-sql-server-2012\/\" title=\"IIF Logical Function in Sql Server 2012\" target=\"_blank\">IIF<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>DATE AND TIME FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\" colspan=\"2\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/22\/eomonth-function-in-sql-server-2012\/\" title=\"EOMONTH Function in Sql Server 2012\" target=\"_blank\">EOMONTH<\/a><\/strong><\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/datefromparts-function-in-sql-server-2012\/\" title=\"DateFromParts Function in Sql Server 2012\" target=\"_blank\">DATEFROMPARTS<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/datetimefromparts-function-in-sql-server-2012\/\" title=\"DateTimeFromParts Function in Sql Server 2012\" target=\"_blank\">DATETIMEFROMPARTS<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/smalldatetimefromparts-function-in-sql-server-2012\/\" title=\"SmallDateTimeFromParts Function in Sql Server 2012\" target=\"_blank\">SMALLDATETIMEFROMPARTS<\/a> <\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>DATETIME2FROMPARTS <\/strong<\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>TIMEFROMPARTS <\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>DATETIMEOFFSETFROMPARTS<\/strong<\/td>\n<tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>PARSE is one of the new built-in conversion function introduced as a Part of Sql Server 2012. PARSE function converts the string expression to the requested data type. It tries it&#8217;s best to translate the string\u00a0value to requested type but if it fails to\u00a0translate then raises an exception. Important Note: This PARSE function is not &hellip; <a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/parse-sql-server-2012-built-in-conversion-function\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PARSE CONVERSION FUNCTION IN SQL SERVER 2012<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[89,13,3,5],"tags":[989,98,96,95,35,94,97,986,55],"class_list":["post-968","post","type-post","status-publish","format-standard","hentry","category-conversion-functions","category-functions","category-sql-server","category-sql-server-2012-sql-server","tag-conversion-functions","tag-convert-vs-parse","tag-difference-between-parse-and-convert","tag-differences-2","tag-new-feature-in-sql-server-2012","tag-parse","tag-parse-vs-conert","tag-sql-server","tag-sql-server-2012"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-fC","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=968"}],"version-history":[{"count":23,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/968\/revisions"}],"predecessor-version":[{"id":992,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/968\/revisions\/992"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}