{"id":668,"date":"2012-12-02T02:35:48","date_gmt":"2012-12-01T21:05:48","guid":{"rendered":"http:\/\/beginsql.wordpress.com\/?p=668"},"modified":"2013-06-22T17:35:11","modified_gmt":"2013-06-22T12:05:11","slug":"concat-function-in-sql-server-2012","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2012\/12\/02\/concat-function-in-sql-server-2012\/","title":{"rendered":"CONCAT() STRING FUNCTION IN SQL Server 2012"},"content":{"rendered":"<p>This is one of the new string functions introduced in SQL Server 2012. This function provides an easy way for the developers to concatenate the string values. Let us first see the syntax of this method:<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p><em>CONCAT( string1, string2 [,stringN])<\/em><\/p>\n<p>Here the parameters: string1, string2, &#8230; ,stringN are the string values which will be concatenated. This method requires\u00a0minimum\u00a0two arguments and max 254 arguments.<\/p>\n<p>Let us understand this new CONCAT function\u00a0 with a simple example:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">SELECT CONCAT(&#039;Basavaraj&#039;,&#039; &#039;,&#039;Biradar&#039;) Name<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\nName<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nBasavaraj Biradar<\/p>\n<p>Some of the important features about this function are:<br \/>\n<strong><span style=\"color: #0000ff;\">1) All arguments are implicitly converted to string types before concatenating them.<\/span><\/strong><\/p>\n<p>Let us see this with an example:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">DECLARE @EmployeeId INT = 1\r\nSELECT CONCAT(@EmployeeId,&#039; &#039;,&#039;Basavaraj&#039;) EmployeeIDAndName<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\nEmployeeIDAndName<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n1 Basavaraj<\/p>\n<p>In previous versions of SQL Server where if we are using &#8216;+&#8217; to concatenate the values then we were\u00a0explicitly\u00a0need to convert them to string and then concatenate. To achieve the result as in the above SQL 2012 \u00a0example we were needed to write a Sql Statement like below with CAST\/CONVERT function:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">DECLARE @EmployeeId INT = 1\r\nSELECT CAST(@EmployeeId AS VARCHAR) + &#039; &#039; \r\n             + &#039;Basavaraj&#039; EmployeeIDAndName\r\n<\/pre>\n<p><strong><span style=\"color: #0000ff;\">2) Null values are implicitly converted to an empty string.<\/span><\/strong><\/p>\n<p>Let us see this with an example:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\nDECLARE @FirstName VARCHAR(20) = &#039;Basavaraj&#039;, \r\n        @LastName VARCHAR(20)\r\nSELECT CONCAT(@FirstName, &#039; &#039;,@LastName) Name<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\nName<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nBasavaraj<\/p>\n<p>In previous versions of SQL Server where if we are using &#8216;+&#8217; to concatenate the values then we know that if one of the values is NULL then concatenated value will be NULL. To avoid this we were needed to use ISNULL function to check if the value is null then consider it as empty string. To achieve the result as in the above SQL 2012 \u00a0example we were needed to write a Sql Statement like below with ISNULL function:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\nDECLARE @FirstName VARCHAR(20) = &#039;Basavaraj&#039;, \r\n        @LastName VARCHAR(20)\r\nSELECT ISNULL(@FirstName,&#039;&#039;) + &#039; &#039; \r\n       + ISNULL(@LastName,&#039;&#039;) Name<\/pre>\n<p><span style=\"color: #ff6600;\"><strong>QUIZ:<\/strong><\/span><br \/>\nWhether the below query will return all the 8000 characters &#8216;B&#8217; and 8000 characters &#8216;C&#8217; in the result?<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">DECLARE @string1 AS VARCHAR(8000) = REPLICATE(&#039;B&#039;,8000),\r\n @string2 AS VARCHAR(8000) = REPLICATE(&#039;C&#039;,8000)\r\nSELECT CONCAT(@string1,@string2)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is one of the new string functions introduced in SQL Server 2012. This function provides an easy way for the developers to concatenate the string values. Let us first see the syntax of this method: Syntax: CONCAT( string1, string2 [,stringN]) Here the parameters: string1, string2, &#8230; ,stringN are the string values which will be &hellip; <a href=\"https:\/\/sqlhints.com\/2012\/12\/02\/concat-function-in-sql-server-2012\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CONCAT() STRING FUNCTION IN SQL Server 2012<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13,3,5],"tags":[17,55,58],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-functions","category-sql-server","category-sql-server-2012-sql-server","tag-concat","tag-sql-server-2012","tag-string-functions"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-aM","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/668","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":3,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":1117,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/668\/revisions\/1117"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}