{"id":5270,"date":"2013-04-14T00:00:38","date_gmt":"2013-04-14T00:00:38","guid":{"rendered":"http:\/\/craftydba.com\/?p=5270"},"modified":"2017-10-11T16:11:56","modified_gmt":"2017-10-11T16:11:56","slug":"string-functions-patindex","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5270","title":{"rendered":"String Functions &#8211; PATINDEX()"},"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 PATINDEX() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188395.aspx\">PATINDEX()<\/a> function takes a search pattern to find and a expression to search as input parameters. It returns the first position in which the search pattern was found.  Unlike the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186323.aspx\">CHARINDEX()<\/a> function, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms179884.aspx\">wild card characters<\/a> such as %, _, and ^ can be used used to define the pattern.  In a nutshell, it is a fuzzy match that is performed.<\/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 any word that starts with <b>tr<\/b>.<\/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 - patindex()\">\r\n\r\n-- Set local variables\r\ndeclare @var_jingle varchar(512)\r\n    = 'silly rabbit trix are for kids';\r\ndeclare @var_find varchar(16)\r\n    = '%rabbit%';\r\n\r\n-- Find location of rabbit (7)\r\nselect patindex(@var_find, @var_jingle) as pos1;\r\n\r\n-- Find location of rabies (0)\r\nselect patindex('%rabies%', @var_jingle) as pos2;\r\n\r\n-- Find location of any word starting with tr, 14\r\nselect patindex('%tr__%', @var_jingle) as pos3;\r\n\r\n-- Null search string, NULL\r\nselect patindex(NULL, @var_jingle) as pos4;\r\n\r\n-- Null source string, error\r\nselect patindex(@var_find, NULL) as pos5;\r\n\r\n<\/pre>\n<\/p>\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;\">14 &#8211; Expression is matched to trix.<\/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;\">ERROR &#8211; This function call generates an error.<\/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\n14\r\n\r\npos4\r\n-----------\r\nNULL\r\n\r\nMsg 8116, Level 16, State 1, Line 20\r\nArgument data type NULL is invalid for argument 2 of patindex function.\r\n<\/pre>\n<\/p>\n<p>Next time, I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms176114.aspx\">QUOTENAME()<\/a> function that comes in handy when creating dynamic TSQL with object names that might need to be quoted.<\/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 PATINDEX() function today. The PATINDEX() function takes a search pattern to find and a expression to search as input parameters. It returns the first position in which the search pattern was found. Unlike the CHARINDEX() function, wild card characters such as %, _, and ^ can be used used to define the pattern. In a nutshell, it is a fuzzy match that is performed. The following example is&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,823,815,29],"class_list":["post-5270","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-patindex","tag-string-function","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5270","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=5270"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5270\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}