{"id":1270,"date":"2020-11-17T10:00:07","date_gmt":"2020-11-17T15:00:07","guid":{"rendered":"https:\/\/novicksoftware.com\/?p=1270"},"modified":"2020-11-18T09:02:06","modified_gmt":"2020-11-18T14:02:06","slug":"improve-sql-string-parsing","status":"publish","type":"post","link":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/","title":{"rendered":"Improve SQL String Parsing"},"content":{"rendered":"<p>The number of ways to receive information seems to increase all the time. One of the ways to get information from the web is an RSS feed. If you use a feed reader.<\/p>\n<p>This issue features a contribution from reader Nick Barclay from Australia. The function, InString, is one he&#8217;s found useful for parsing strings into their parts. The function returns the character position of the character after the Nth instance of the search string. It&#8217;s useful for parsing delimited strings. Here&#8217;s the CREATE FUNCTION script:<\/p>\n<div class=\"code\">SET QUOTED_IDENTIFIER ON<br \/>\nSET ANSI_NULLS ON<br \/>\nSET NOCOUNT ON<br \/>\nGOCREATE function InString(@string varchar(200),<br \/>\n@searchfor varchar(50),<br \/>\n@position int<br \/>\n) returns int<\/p>\n<p>\/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br \/>\nReturns the position of the character AFTER the nth instance<br \/>\nof the string<\/p>\n<p>&#8212; TEST CASE #1<br \/>\n&#8212; should return 18<br \/>\nselect dbo.InString(&#8216;123456 123456 123456 123456&#8242;, &#8217;23&#8217;, 3)<br \/>\nas [Test Case #1]<\/p>\n<p>&#8212; TEST CASE #2<br \/>\n&#8212; should return 25<br \/>\nselect dbo.InString(&#8216;test1\/test2\/test3\/test4\/test5\/&#8217;, &#8216;\/&#8217;, 4)<br \/>\nas [Test Case #2]<\/p>\n<p>&#8212; TEST CASE #3<br \/>\ndeclare @teststring varchar(50)<br \/>\nset @teststring = &#8216;test1\/test2\/test3\/test4\/test5\/&#8217;<br \/>\nselect substring(@teststring, dbo.Instring(@teststring,&#8217;\/&#8217;,3),5)<br \/>\nas [Test Case #3]<br \/>\n&#8212; should return &#8216;test4&#8217;<\/p>\n<p>&#8212; TEST CASE #4 (variable length delimited fields<br \/>\ndeclare @teststring2 varchar(50)<br \/>\nset @teststring2 = &#8216;test123\/test\/testtestestest\/testxyz\/test\/&#8217;<br \/>\nselect substring( @teststring2,<br \/>\ndbo.Instring(@teststring2, &#8216;\/&#8217;, 3),<br \/>\n(dbo.Instring(@teststring2, &#8216;\/&#8217;, 4) -1)<br \/>\n&#8211; dbo.Instring(@teststring2, &#8216;\/&#8217;, 3)<br \/>\n) as [Test Case #4]<br \/>\n&#8212; should return &#8216;testxyz&#8217;<br \/>\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*\/<\/p>\n<p>as<br \/>\nbegin<br \/>\ndeclare @lenstring int, @poscount int, @stringpos int<br \/>\nset @lenstring = datalength(@searchfor)<br \/>\nset @poscount = 1<br \/>\nset @stringpos = 1<br \/>\nwhile @poscount &lt;= @position and @stringpos &lt;= len(@string)<br \/>\nbegin<br \/>\n&#8212; if we find the string segment we&#8217;re looking for<br \/>\nif substring(@string, @stringpos, @lenstring)=@searchfor<br \/>\nbegin<br \/>\n&#8212; is the instance of the string the one we are<br \/>\n&#8212; looking for?<br \/>\nif @poscount = @position<br \/>\nbegin<br \/>\nset @stringpos = @stringpos + @lenstring<br \/>\nreturn @stringpos<br \/>\nend<br \/>\n&#8212; else look for the next instance of the string<br \/>\n&#8212; segment<br \/>\nelse<br \/>\nbegin<br \/>\nset @poscount = @poscount + 1<br \/>\nend<br \/>\nend<br \/>\nset @stringpos = @stringpos + 1<br \/>\nend<br \/>\nreturn null<br \/>\nend<br \/>\nGO<\/p>\n<p>GRANT EXEC on dbo.InString TO PUBLIC<br \/>\nGO<\/p>\n<\/div>\n<p>The tests from the function header illustrate how the function works so lets use them as the demonstration:<\/p>\n<div class=\"code\">&#8212; TEST CASE #1<br \/>\n&#8212; should return 18<br \/>\nselect dbo.InString(&#8216;123456 123456 123456 123456&#8242;, &#8217;23&#8217;, 3)<br \/>\nas [Test Case #1]&#8211; TEST CASE #2<br \/>\n&#8212; should return 25<br \/>\nselect dbo.InString(&#8216;test1\/test2\/test3\/test4\/test5\/&#8217;, &#8216;\/&#8217;, 4)<br \/>\nas [Test Case #2]&#8211; TEST CASE #3<br \/>\ndeclare @teststring varchar(50)<br \/>\nset @teststring = &#8216;test1\/test2\/test3\/test4\/test5\/&#8217;<br \/>\nselect substring(@teststring, dbo.Instring(@teststring,&#8217;\/&#8217;,3),5)<br \/>\nas [Test Case #3]<br \/>\n&#8212; should return &#8216;test4&#8217;<\/p>\n<p>&#8212; TEST CASE #4 (variable length delimited fields<br \/>\ndeclare @teststring2 varchar(50)<br \/>\nset @teststring2 = &#8216;test123\/test\/testtestestest\/testxyz\/test\/&#8217;<br \/>\nselect substring( @teststring2,<br \/>\ndbo.Instring(@teststring2, &#8216;\/&#8217;, 3),<br \/>\n(dbo.Instring(@teststring2, &#8216;\/&#8217;, 4) -1)<br \/>\n&#8211; dbo.Instring(@teststring2, &#8216;\/&#8217;, 3)<br \/>\n) as [Test Case #4]<br \/>\n&#8212; should return &#8216;testxyz&#8217;<br \/>\nGO<\/p>\n<\/div>\n<p>Results:<\/p>\n<table>\n<tbody>\n<tr>\n<th>Test Case #1<\/th>\n<\/tr>\n<tr>\n<td>18<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<th>Test Case #2<\/th>\n<\/tr>\n<tr>\n<td>25<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<th>Test Case #3<\/th>\n<\/tr>\n<tr>\n<td>test4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<th>Test Case #4<\/th>\n<\/tr>\n<tr>\n<td>testxyz<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Test cases 3 and 4 really illustrate how to use the function in a real situation. Take a closer look at test 3:<\/p>\n<div class=\"code\">declare @teststring varchar(50) set @teststring = &#8216;test1\/test2\/test3\/test4\/test5\/&#8217; select substring(@teststring, dbo.Instring(@teststring,&#8217;\/&#8217;,3),5) as [Test Case #3]<\/div>\n<p>In this instance, we have strings that are five characters separated by backslash (\/). The expression returns the string after the 3rd backslash.<\/p>\n<p>Thanks to Nick for his contribution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The number of ways to receive information seems to increase all the time. One of the ways to get information from the web is an RSS feed. If you use a feed reader. This issue features a contribution from reader Nick Barclay from Australia. The function, InString, is one he&#8217;s found useful for parsing strings [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","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","enabled":false},"version":2}},"categories":[6],"tags":[],"class_list":["post-1270","post","type-post","status-publish","format-standard","hentry","category-sql-coding"],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Improve SQL String Parsing - Novick Software<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Improve SQL String Parsing - Novick Software\" \/>\n<meta property=\"og:description\" content=\"The number of ways to receive information seems to increase all the time. One of the ways to get information from the web is an RSS feed. If you use a feed reader. This issue features a contribution from reader Nick Barclay from Australia. The function, InString, is one he&#8217;s found useful for parsing strings [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/\" \/>\n<meta property=\"og:site_name\" content=\"Novick Software\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-17T15:00:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-18T14:02:06+00:00\" \/>\n<meta name=\"author\" content=\"Ian Bachman-Sanders\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ian Bachman-Sanders\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/\"},\"author\":{\"name\":\"Ian Bachman-Sanders\",\"@id\":\"https:\/\/novicksoftware.com\/#\/schema\/person\/d8ba84bd499de646b3ee46a5b0e20224\"},\"headline\":\"Improve SQL String Parsing\",\"datePublished\":\"2020-11-17T15:00:07+00:00\",\"dateModified\":\"2020-11-18T14:02:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/\"},\"wordCount\":551,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/novicksoftware.com\/#organization\"},\"articleSection\":[\"SQL coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/\",\"url\":\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/\",\"name\":\"Improve SQL String Parsing - Novick Software\",\"isPartOf\":{\"@id\":\"https:\/\/novicksoftware.com\/#website\"},\"datePublished\":\"2020-11-17T15:00:07+00:00\",\"dateModified\":\"2020-11-18T14:02:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/novicksoftware.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Improve SQL String Parsing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/novicksoftware.com\/#website\",\"url\":\"https:\/\/novicksoftware.com\/\",\"name\":\"Novick Software\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/novicksoftware.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/novicksoftware.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/novicksoftware.com\/#organization\",\"name\":\"Novick Software\",\"url\":\"https:\/\/novicksoftware.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/novicksoftware.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/secureservercdn.net\/198.71.233.185\/o7v.274.myftpupload.com\/wp-content\/uploads\/2014\/07\/novick_header_wide1.jpg?time=1618718701\",\"contentUrl\":\"https:\/\/secureservercdn.net\/198.71.233.185\/o7v.274.myftpupload.com\/wp-content\/uploads\/2014\/07\/novick_header_wide1.jpg?time=1618718701\",\"width\":1140,\"height\":100,\"caption\":\"Novick Software\"},\"image\":{\"@id\":\"https:\/\/novicksoftware.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/novicksoftware.com\/#\/schema\/person\/d8ba84bd499de646b3ee46a5b0e20224\",\"name\":\"Ian Bachman-Sanders\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/novicksoftware.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c7440c74041bd8c4961cf8a2b3a1f962e7808b48c7fffe6b758314cce008efe4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c7440c74041bd8c4961cf8a2b3a1f962e7808b48c7fffe6b758314cce008efe4?s=96&d=mm&r=g\",\"caption\":\"Ian Bachman-Sanders\"},\"url\":\"https:\/\/novicksoftware.com\/author\/ns-ian-bs\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Improve SQL String Parsing - Novick Software","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/","og_locale":"en_US","og_type":"article","og_title":"Improve SQL String Parsing - Novick Software","og_description":"The number of ways to receive information seems to increase all the time. One of the ways to get information from the web is an RSS feed. If you use a feed reader. This issue features a contribution from reader Nick Barclay from Australia. The function, InString, is one he&#8217;s found useful for parsing strings [&hellip;]","og_url":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/","og_site_name":"Novick Software","article_published_time":"2020-11-17T15:00:07+00:00","article_modified_time":"2020-11-18T14:02:06+00:00","author":"Ian Bachman-Sanders","twitter_misc":{"Written by":"Ian Bachman-Sanders","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#article","isPartOf":{"@id":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/"},"author":{"name":"Ian Bachman-Sanders","@id":"https:\/\/novicksoftware.com\/#\/schema\/person\/d8ba84bd499de646b3ee46a5b0e20224"},"headline":"Improve SQL String Parsing","datePublished":"2020-11-17T15:00:07+00:00","dateModified":"2020-11-18T14:02:06+00:00","mainEntityOfPage":{"@id":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/"},"wordCount":551,"commentCount":0,"publisher":{"@id":"https:\/\/novicksoftware.com\/#organization"},"articleSection":["SQL coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/","url":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/","name":"Improve SQL String Parsing - Novick Software","isPartOf":{"@id":"https:\/\/novicksoftware.com\/#website"},"datePublished":"2020-11-17T15:00:07+00:00","dateModified":"2020-11-18T14:02:06+00:00","breadcrumb":{"@id":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/novicksoftware.com\/improve-sql-string-parsing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/novicksoftware.com\/improve-sql-string-parsing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/novicksoftware.com\/"},{"@type":"ListItem","position":2,"name":"Improve SQL String Parsing"}]},{"@type":"WebSite","@id":"https:\/\/novicksoftware.com\/#website","url":"https:\/\/novicksoftware.com\/","name":"Novick Software","description":"","publisher":{"@id":"https:\/\/novicksoftware.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/novicksoftware.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/novicksoftware.com\/#organization","name":"Novick Software","url":"https:\/\/novicksoftware.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/novicksoftware.com\/#\/schema\/logo\/image\/","url":"https:\/\/secureservercdn.net\/198.71.233.185\/o7v.274.myftpupload.com\/wp-content\/uploads\/2014\/07\/novick_header_wide1.jpg?time=1618718701","contentUrl":"https:\/\/secureservercdn.net\/198.71.233.185\/o7v.274.myftpupload.com\/wp-content\/uploads\/2014\/07\/novick_header_wide1.jpg?time=1618718701","width":1140,"height":100,"caption":"Novick Software"},"image":{"@id":"https:\/\/novicksoftware.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/novicksoftware.com\/#\/schema\/person\/d8ba84bd499de646b3ee46a5b0e20224","name":"Ian Bachman-Sanders","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/novicksoftware.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c7440c74041bd8c4961cf8a2b3a1f962e7808b48c7fffe6b758314cce008efe4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c7440c74041bd8c4961cf8a2b3a1f962e7808b48c7fffe6b758314cce008efe4?s=96&d=mm&r=g","caption":"Ian Bachman-Sanders"},"url":"https:\/\/novicksoftware.com\/author\/ns-ian-bs\/"}]}},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4VNnB-ku","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/posts\/1270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/comments?post=1270"}],"version-history":[{"count":4,"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/posts\/1270\/revisions"}],"predecessor-version":[{"id":2164,"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/posts\/1270\/revisions\/2164"}],"wp:attachment":[{"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/media?parent=1270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/categories?post=1270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/novicksoftware.com\/wp-json\/wp\/v2\/tags?post=1270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}