{"id":542,"date":"2012-06-21T17:41:10","date_gmt":"2012-06-21T12:41:10","guid":{"rendered":"http:\/\/www.esearing.com\/v2010\/?p=542"},"modified":"2012-10-07T19:05:11","modified_gmt":"2012-10-07T14:05:11","slug":"sql-patindex-and-case","status":"publish","type":"post","link":"https:\/\/www.esearing.com\/v2010\/2012\/06\/sql-patindex-and-case\/","title":{"rendered":"SQL &#8211; PATINDEX and CASE"},"content":{"rendered":"<p>I had the odd challenge of providing an account number by removing any values that were the same except for numbers at the end.<br \/>\nExamples  custname01, custname02, custname03, etc.<br \/>\nSince these accounts are all really the same customer we need to summarize a report by custname, without the numbers.<br \/>\nHowever some accounts legitimately start with numbers and are truly different accounts. This came about because of a merger of two different systems.<br \/>\nExamples. 09876-a,  09876-b, etc<\/p>\n<p><strong>Here is my solution:<\/strong><br \/>\nUse CASE and PATINDEX to separate the accounts into their distinct appropriate groups and create a filed called rollup_account that is the summary point. PATINDEX will show the first occurrence of a string defined and return its position value. So if a number is found after the first position I want to return the alpha string before the numbers. If it starts with a number just report the original account value; PATINDEX will return zero if a number is found as the first character in the string.<\/p>\n<p><code><br \/>\nSELECT account,<br \/>\nrollup_account = CASE<br \/>\n  IF (PATINDEX('%[0-9]%', account) > 0)<br \/>\n  THEN LEFT(account, (PATINDEX('%[0-9]%', account)-1)<br \/>\n  ELSE account<br \/>\n  END;<br \/>\nfrom tablename ...<br \/>\n<\/code><\/p>\n<p>OUTPUT<br \/>\naccount    | rollup_account<br \/>\ncustname01 | custname<br \/>\ncustname02 | custname<br \/>\ncustname03 | custname<br \/>\n09876-a    | 09876-a<br \/>\n09876-b    | 09876-b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had the odd challenge of providing an account number by removing any values that were the same except for numbers at the end. Examples custname01, custname02, custname03, etc. Since these accounts are all really the same customer we need &hellip; <a href=\"https:\/\/www.esearing.com\/v2010\/2012\/06\/sql-patindex-and-case\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,14],"tags":[],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-problems-fixes","category-sql"],"_links":{"self":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/comments?post=542"}],"version-history":[{"count":3,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":587,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/542\/revisions\/587"}],"wp:attachment":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}