{"id":149,"date":"2007-09-18T19:28:54","date_gmt":"2007-09-19T02:28:54","guid":{"rendered":"http:\/\/jeffhobbs.net\/2007\/09\/18\/some-sql-for-the-day-2\/"},"modified":"2007-09-18T19:28:54","modified_gmt":"2007-09-19T02:28:54","slug":"some-sql-for-the-day-2","status":"publish","type":"post","link":"https:\/\/jeffhobbs.com\/?p=149","title":{"rendered":"Some SQL For the Day"},"content":{"rendered":"<p>We maintain a column in the GIS that tracks the billing number for each facility (asset) we put into the ground. Although there are a large number of billing numbers (probably several thousand at this point), they should all follow the same pattern. To ensure there are any errors when entering them into the GIS, I wrote a check constraint. For those that aren&#8217;t familiar with check constraints in Oracle, they&#8217;re basically a step below referential integrity. They allow any value to be put into a column as long as the value meets the rules of the check constraint.<\/p>\n<p>So in my case the billing number is a seven character number following this format &quot;G6-0901&quot;. So here&#8217;s the constraint that I wrote:<\/p>\n<p><strong>REGEXP_LIKE (ECM_NUMBER,'[E][6-9]-[0][8-9][0-9][0-9]&#8217;) OR REGEXP_LIKE (ECM_NUMBER,'[G][0-9]-[0][8-9][0-9][0-9]&#8217;)<\/strong><\/p>\n<p>REGEXP_LIKE &#8211; This is a new function release in Oracle 10g. It allows you to use regular expressions in your SQL. In this case, I&#8217;m using an Oracle function &quot;REGEXP_LIKE&quot; which allows you to compare a known value against a regular expressiont.<\/p>\n<p>ECM_NUMBER &#8211; This is simply the column name where the constraint resides. In other words, this is the name of the column that holds the billing number.<\/p>\n<p>[E] &#8211; This ensures that the first character in the number is the letter &quot;E&quot;<\/p>\n<p>[6-9] &#8211; This ensures that the second character in the number is a number 6, 7, 8, or 9<\/p>\n<p>&quot;-&quot; &#8211; This ensures that the third character is a &quot;dash&quot;<\/p>\n<p>[0] &#8211; This ensures the fourth character is a 0<\/p>\n<p>[8-9] &#8211; This ensures the fifth character is a 8 or 9<\/p>\n<p>[0-9] &#8211; This ensures that the sixth character is a 0 &#8211; 9<\/p>\n<p>[0-9] &#8211; See above<\/p>\n<p>Now I&#8217;ve also placed an OR statement in there providing two different formatting options. The rules are the same, but I&#8217;m just allowing for different formatting (e.g. starts with a &quot;G&quot; as opposed to an &quot;E&quot;), etc. I suppose I could have written it into one expression, it was just easier (in my eyes) to put it into two expressions.<\/p>\n<p>Overall, I think it&#8217;s also pretty cool that really almost all of this is just regular expressions. So, if you&#8217;re familiar with regular expressions and their syntax, these types of constraints (and even MUCH more advanced queries) should be very &quot;simple&quot;. \n\t\t\t          <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We maintain a column in the GIS that tracks the billing number for each facility (asset) we put into the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[11],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Some SQL For the Day - jeffhobbs.com<\/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:\/\/jeffhobbs.com\/?p=149\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Some SQL For the Day - jeffhobbs.com\" \/>\n<meta property=\"og:description\" content=\"We maintain a column in the GIS that tracks the billing number for each facility (asset) we put into the...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jeffhobbs.com\/?p=149\" \/>\n<meta property=\"og:site_name\" content=\"jeffhobbs.com\" \/>\n<meta property=\"article:published_time\" content=\"2007-09-19T02:28:54+00:00\" \/>\n<meta name=\"author\" content=\"Jeff Hobbs\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeff Hobbs\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jeffhobbs.com\/?p=149\",\"url\":\"https:\/\/jeffhobbs.com\/?p=149\",\"name\":\"Some SQL For the Day - jeffhobbs.com\",\"isPartOf\":{\"@id\":\"https:\/\/jeffhobbs.com\/#website\"},\"datePublished\":\"2007-09-19T02:28:54+00:00\",\"dateModified\":\"2007-09-19T02:28:54+00:00\",\"author\":{\"@id\":\"https:\/\/jeffhobbs.com\/#\/schema\/person\/600c6cc6bd115876f071a9bfb5c934d7\"},\"breadcrumb\":{\"@id\":\"https:\/\/jeffhobbs.com\/?p=149#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jeffhobbs.com\/?p=149\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jeffhobbs.com\/?p=149#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jeffhobbs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Some SQL For the Day\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jeffhobbs.com\/#website\",\"url\":\"https:\/\/jeffhobbs.com\/\",\"name\":\"jeffhobbs.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jeffhobbs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/jeffhobbs.com\/#\/schema\/person\/600c6cc6bd115876f071a9bfb5c934d7\",\"name\":\"Jeff Hobbs\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jeffhobbs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/bafb9a6abd79509b50a190d399dea779?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/bafb9a6abd79509b50a190d399dea779?s=96&d=mm&r=g\",\"caption\":\"Jeff Hobbs\"},\"sameAs\":[\"http:\/\/jeffhobbs.com\"],\"url\":\"https:\/\/jeffhobbs.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Some SQL For the Day - jeffhobbs.com","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:\/\/jeffhobbs.com\/?p=149","og_locale":"en_US","og_type":"article","og_title":"Some SQL For the Day - jeffhobbs.com","og_description":"We maintain a column in the GIS that tracks the billing number for each facility (asset) we put into the...","og_url":"https:\/\/jeffhobbs.com\/?p=149","og_site_name":"jeffhobbs.com","article_published_time":"2007-09-19T02:28:54+00:00","author":"Jeff Hobbs","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jeff Hobbs","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jeffhobbs.com\/?p=149","url":"https:\/\/jeffhobbs.com\/?p=149","name":"Some SQL For the Day - jeffhobbs.com","isPartOf":{"@id":"https:\/\/jeffhobbs.com\/#website"},"datePublished":"2007-09-19T02:28:54+00:00","dateModified":"2007-09-19T02:28:54+00:00","author":{"@id":"https:\/\/jeffhobbs.com\/#\/schema\/person\/600c6cc6bd115876f071a9bfb5c934d7"},"breadcrumb":{"@id":"https:\/\/jeffhobbs.com\/?p=149#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jeffhobbs.com\/?p=149"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jeffhobbs.com\/?p=149#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jeffhobbs.com\/"},{"@type":"ListItem","position":2,"name":"Some SQL For the Day"}]},{"@type":"WebSite","@id":"https:\/\/jeffhobbs.com\/#website","url":"https:\/\/jeffhobbs.com\/","name":"jeffhobbs.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jeffhobbs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/jeffhobbs.com\/#\/schema\/person\/600c6cc6bd115876f071a9bfb5c934d7","name":"Jeff Hobbs","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jeffhobbs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/bafb9a6abd79509b50a190d399dea779?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bafb9a6abd79509b50a190d399dea779?s=96&d=mm&r=g","caption":"Jeff Hobbs"},"sameAs":["http:\/\/jeffhobbs.com"],"url":"https:\/\/jeffhobbs.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=\/wp\/v2\/posts\/149"}],"collection":[{"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149"}],"version-history":[{"count":0,"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeffhobbs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}