{"id":958,"date":"2013-05-03T16:13:18","date_gmt":"2013-05-03T16:13:18","guid":{"rendered":"http:\/\/andrewpallant.ca\/wordpress\/?p=958"},"modified":"2013-11-26T11:45:02","modified_gmt":"2013-11-26T16:45:02","slug":"sql-function-to-find-a-value-in-a-string-of-values","status":"publish","type":"post","link":"http:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/","title":{"rendered":"SQL Function &#8211; Find a Value in a String of Values"},"content":{"rendered":"<p>In a table I have a field that you can use comma delimited value (example 1,2,3,22). When I tried to filtering use a &#8220;LIKE&#8221; statement on the value 2, I was being returned 2 and 22. ARG!<\/p>\n<p>So what do you do? You create a simple function the returns a 1 or 0. 1 being successfully found and a 0 meaning failed to find. Seems simple enough? Well it was and the function can be easily tweaked to match the solution you need! Here is the function as I used it:<br \/>\n<!--more--><\/p>\n<pre style=\"background-color: f8fcc7; margin-left: 20px; padding: 10px;\">\/****** Object:  UserDefinedFunction [dbo].[isFound]    Script Date: 05\/03\/2013 11:54:23 ******\/\r\nSET ANSI_NULLS ON\r\nGO\r\nSET QUOTED_IDENTIFIER ON\r\nGO\r\n-- =============================================\r\n-- Author:\t\tAndrew Pallant\r\n-- Create date: 03\/05\/2013\r\n-- Description:\tFind a value in a comma delimited string of values\r\n-- =============================================\r\nALTER FUNCTION [dbo].[isFound] \r\n(\r\n\t@SearchString as varchar(200), \r\n\t@SearchValue as varchar(10)\r\n)\r\nRETURNS bit\r\nAS\r\nBEGIN\r\n\tdeclare @rtn int\r\n\tset @rtn = 0\r\n\r\n\t-- Declare the return variable here\r\n\tset @SearchString=rtrim(ltrim(@SearchString)) \r\n\tset @SearchValue=rtrim(ltrim(@SearchValue)) \r\n\tset @SearchString = @SearchString + ','\r\n\r\n\tdeclare @i int   \r\n\tset @i=charindex(',',@SearchString)   \r\n\tdeclare @compValue varchar(10)\r\n\twhile @i&gt;=1 BEGIN\r\n\t\tset @compValue = left(@SearchString,@i-1)\r\n\t\tset @compValue=rtrim(ltrim(@compValue)) \r\n\t\tset @SearchString=substring(@SearchString,@i+1,len(@SearchString)-@i)       \r\n\t\tset @SearchString=rtrim(ltrim(@SearchString)) \r\n\t\tset @i=charindex(',',@SearchString)   \r\n\t\tif @compValue = @SearchValue BEGIN\r\n\t\t\tset @rtn = 1\r\n\t\tEND\r\n\tEND\r\n\r\n\t-- Return the result of the function\r\n\tRETURN @rtn\r\n\r\nEND<\/pre>\n<p>Now I can do a simple select statement and get the correct results.<\/p>\n<pre>select * [table1] where dbo.isFound([fieldname],2) = 1<\/pre>\n<p>I have also used it successfully in select with as a join.<\/p>\n<pre>SELECT * FROM [table1] t1 \r\n   INNER JOIN [table2] t2 on .dbo.isFound(t1.[fiedldname],t2.[fieldname]) = 1<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In a table I have a field that you can use comma delimited value (example 1,2,3,22). When I tried to filtering use a &#8220;LIKE&#8221; statement on the value 2, I was being returned 2 and 22. ARG! So what do you do? You create a simple function the returns a 1 or 0. 1 being &hellip; <a href=\"http:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SQL Function &#8211; Find a Value in a String of Values<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[246],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>SQL Function - Find a Value in a String of Values - LDNDeveloper<\/title>\r\n<meta name=\"description\" content=\"SQL Function - Find a Value in a comma Delitmited String of Values\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"SQL Function - Find a Value in a String of Values - LDNDeveloper\" \/>\r\n<meta property=\"og:description\" content=\"SQL Function - Find a Value in a comma Delitmited String of Values\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\" \/>\r\n<meta property=\"og:site_name\" content=\"LDNDeveloper\" \/>\r\n<meta property=\"article:published_time\" content=\"2013-05-03T16:13:18+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2013-11-26T16:45:02+00:00\" \/>\r\n<meta name=\"author\" content=\"ldnDeveloper\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@LdnDeveloper\" \/>\r\n<meta name=\"twitter:site\" content=\"@LdnDeveloper\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ldnDeveloper\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\"},\"author\":{\"name\":\"ldnDeveloper\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"headline\":\"SQL Function &#8211; Find a Value in a String of Values\",\"datePublished\":\"2013-05-03T16:13:18+00:00\",\"dateModified\":\"2013-11-26T16:45:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\"},\"wordCount\":119,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"keywords\":[\"SQL\"],\"articleSection\":[\"SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\",\"name\":\"SQL Function - Find a Value in a String of Values - LDNDeveloper\",\"isPartOf\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#website\"},\"datePublished\":\"2013-05-03T16:13:18+00:00\",\"dateModified\":\"2013-11-26T16:45:02+00:00\",\"description\":\"SQL Function - Find a Value in a comma Delitmited String of Values\",\"breadcrumb\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/andrewpallant.ca\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Function &#8211; Find a Value in a String of Values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#website\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/\",\"name\":\"LDNDeveloper\",\"description\":\"Learning, Growing and Sharing.\",\"publisher\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\",\"name\":\"ldnDeveloper\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"contentUrl\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"width\":512,\"height\":512,\"caption\":\"ldnDeveloper\"},\"logo\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\"},\"description\":\"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf\",\"sameAs\":[\"http:\/\/www.andrewpallant.ca\",\"https:\/\/x.com\/LdnDeveloper\"],\"url\":\"http:\/\/andrewpallant.ca\/wordpress\/author\/ldndeveloper\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Function - Find a Value in a String of Values - LDNDeveloper","description":"SQL Function - Find a Value in a comma Delitmited String of Values","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:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/","og_locale":"en_US","og_type":"article","og_title":"SQL Function - Find a Value in a String of Values - LDNDeveloper","og_description":"SQL Function - Find a Value in a comma Delitmited String of Values","og_url":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/","og_site_name":"LDNDeveloper","article_published_time":"2013-05-03T16:13:18+00:00","article_modified_time":"2013-11-26T16:45:02+00:00","author":"ldnDeveloper","twitter_card":"summary_large_image","twitter_creator":"@LdnDeveloper","twitter_site":"@LdnDeveloper","twitter_misc":{"Written by":"ldnDeveloper","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#article","isPartOf":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/"},"author":{"name":"ldnDeveloper","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"headline":"SQL Function &#8211; Find a Value in a String of Values","datePublished":"2013-05-03T16:13:18+00:00","dateModified":"2013-11-26T16:45:02+00:00","mainEntityOfPage":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/"},"wordCount":119,"commentCount":0,"publisher":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"keywords":["SQL"],"articleSection":["SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/","url":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/","name":"SQL Function - Find a Value in a String of Values - LDNDeveloper","isPartOf":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#website"},"datePublished":"2013-05-03T16:13:18+00:00","dateModified":"2013-11-26T16:45:02+00:00","description":"SQL Function - Find a Value in a comma Delitmited String of Values","breadcrumb":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/andrewpallant.ca\/wordpress\/sql-function-to-find-a-value-in-a-string-of-values\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/andrewpallant.ca\/wordpress\/"},{"@type":"ListItem","position":2,"name":"SQL Function &#8211; Find a Value in a String of Values"}]},{"@type":"WebSite","@id":"https:\/\/andrewpallant.ca\/wordpress\/#website","url":"https:\/\/andrewpallant.ca\/wordpress\/","name":"LDNDeveloper","description":"Learning, Growing and Sharing.","publisher":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84","name":"ldnDeveloper","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","contentUrl":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","width":512,"height":512,"caption":"ldnDeveloper"},"logo":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/"},"description":"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf","sameAs":["http:\/\/www.andrewpallant.ca","https:\/\/x.com\/LdnDeveloper"],"url":"http:\/\/andrewpallant.ca\/wordpress\/author\/ldndeveloper\/"}]}},"_links":{"self":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/958"}],"collection":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=958"}],"version-history":[{"count":21,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/958\/revisions"}],"predecessor-version":[{"id":1101,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/958\/revisions\/1101"}],"wp:attachment":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=958"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}