{"id":5190,"date":"2013-04-05T00:00:00","date_gmt":"2013-04-05T00:00:00","guid":{"rendered":"http:\/\/craftydba.com\/?p=5190"},"modified":"2017-10-11T16:26:58","modified_gmt":"2017-10-11T16:26:58","slug":"string-function-charindex","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5190","title":{"rendered":"String Function &#8211; CHARINDEX()"},"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 CHARINDEX() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186323.aspx\">CHARINDEX()<\/a> function takes a expression to find, a expression to search and a optional start position. It returns the first position in which the search string was found.<\/p>\n<p>The following example is based upon the television <a href=\"http:\/\/en.wikipedia.org\/wiki\/Jingle\">jingle<\/a> that I heard as a kid for <a href=\"http:\/\/en.wikipedia.org\/wiki\/Trix_(cereal)\">Trix<\/a> cereal.<\/p>\n<p>The example below contains four test cases:<\/p>\n<ol>\n<li><span style=\"line-height: 1.6em;\">Word is found in target string.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">Word is not found in target string.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">Starting position &gt; length of string.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">NULL expression to find.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">NULL expression to search.<\/span><\/li>\n<\/ol>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - charindex()\">\r\n-- Local variables\r\ndeclare @var_jingle varchar(512);\r\ndeclare @var_find varchar(16);\r\n\r\n-- Set the strings\r\nselect @var_jingle = 'silly rabbit trix are for kids';\r\nselect @var_find = 'rabbit';\r\n\r\n-- Find location of rabbit, start at front of string\r\nselect charindex(@var_find, @var_jingle) as pos1;\r\n\r\n-- Find location of rabies, start at front of string\r\nselect charindex('rabies', @var_jingle) as pos2;\r\n\r\n-- Find location of rabbit, start at end of string\r\nselect charindex(@var_find, @var_jingle, 31) as pos3;\r\n\r\n-- Null search string\r\nselect charindex(NULL, @var_jingle) as pos4;\r\n\r\n-- Null source string\r\nselect charindex(@var_find, NULL) as pos5;\r\n<\/pre>\n<p>The results of four test cases are the following:<\/p>\n<ol>\n<li><span style=\"line-height: 1.6em;\">7 &#8211; Seventh character position is start of string.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">0 &#8211; Expression to find not found.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">0 &#8211; Expression to find not found.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">NULL &#8211; Position is unknown.<\/span><\/li>\n<li><span style=\"line-height: 1.6em;\">NULL &#8211; Position is unknown.<\/span><\/li>\n<\/ol>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\npos1\r\n-----------\r\n7\r\n\r\npos2\r\n-----------\r\n0\r\n\r\npos3\r\n-----------\r\n0\r\n\r\npos4\r\n-----------\r\nNULL\r\n\r\npos5\r\n-----------\r\nNULL\r\n\r\n<\/pre>\n<p>Next time, I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh231515.aspx\">CONCAT()<\/a> function.  This new function was introduction in SQL Server 2012.<\/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 CHARINDEX() function today. The CHARINDEX() function takes a expression to find, a expression to search and a optional start position. It returns the first position in which the search string was found. The following example is based upon the television jingle that I heard as a kid for Trix cereal. The example below contains four test cases: Word is found in target string. Word is not found in&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":[738,31,15,815,29],"class_list":["post-5190","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-charindex","tag-database-developer","tag-john-f-miner-iii","tag-string-function","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5190","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=5190"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5190\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}