{"id":5309,"date":"2013-04-17T00:00:08","date_gmt":"2013-04-17T00:00:08","guid":{"rendered":"http:\/\/craftydba.com\/?p=5309"},"modified":"2017-10-08T16:32:17","modified_gmt":"2017-10-08T16:32:17","slug":"string-functions-replicate","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5309","title":{"rendered":"String Functions &#8211; REPLICATE()"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-5158\" title=\"turquoise-yarn-md\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md-150x150.png\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><br \/>\nI am going to continue my series of very short articles or tidbits on Transaction SQL string functions. I will exploring the REPLICATE() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms174383.aspx\">REPLICATE()<\/a> function takes a pattern string [S] and a integer replication count [C] as input.  The function returns a string composed of pattern string [S] concatenated [C] times to itself as output.<\/p>\n<p>The example below replicates a pattern &#8216;XO&#8217; five times.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - replicate()\">\r\n-- A string of XO's\r\nselect replicate('XO', 5) as str_love;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_love\r\n---------\r\nXOXOXOXOXO\r\n<\/pre>\n<\/p>\n<p>One thing to always worry about when using TSQL functions is how will it react with a EMPTY strings or NULL values?  Both empty strings and NULL values as input return themselves as output.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - replicate()\">\r\n-- Empty string returns empty string\r\nselect replicate('', 10) as str_empty;\r\n\r\n-- Null returns Null\r\nselect replicate(NULL, 10) as str_null;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_empty\r\n----------\r\n\r\nstr_null\r\n---------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>Another thing to worry about when using TSQL functions is how will it react with out of range values.  In the example below, a negative replication count returns a NULL string.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - replicate()\">\r\n-- Invalid length returns NULL\r\nselect replicate('-', -10) as str_dashes;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_dashes\r\n----------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>Next time, I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms180040.aspx\">REVERSE()<\/a> function.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to continue my series of very short articles or tidbits on Transaction SQL string functions. I will exploring the REPLICATE() function today. The REPLICATE() function takes a pattern string [S] and a integer replication count [C] as input. The function returns a string composed of pattern string [S] concatenated [C] times to itself as output. The example below replicates a pattern &#8216;XO&#8217; five times. &#8212; A string of XO&#8217;s select replicate(&#8216;XO&#8217;, 5) as str_love; output: str_love &#8212;&#8212;&#8212; XOXOXOXOXO One thing to always worry about when using TSQL&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[31,15,615,815,29],"class_list":["post-5309","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-replicate","tag-string-function","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5309"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5309\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}