{"id":1010,"date":"2013-08-27T16:57:07","date_gmt":"2013-08-27T16:57:07","guid":{"rendered":"http:\/\/andrewpallant.ca\/wordpress\/?p=1010"},"modified":"2013-11-26T11:42:19","modified_gmt":"2013-11-26T16:42:19","slug":"sql-extract-number","status":"publish","type":"post","link":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/","title":{"rendered":"How to use SQL to Extract a Number from a String"},"content":{"rendered":"<p>Recently I had a need to extract the number from a string in the database. \u00a0The string would be something like &#8216;Monitor 16&#8243;&#8216; or &#8220;16 inch Monitor&#8221;. \u00a0 I would need to get the size for various reasons including fees and reports, but no real good way of doing it. \u00a0I have come up with the following Scalar SQL Function to do this very job.<\/p>\n<p><!--more--><\/p>\n<pre style=\"background-color: f8fcc7; margin-left: 20px; padding: 10px;\">SET ANSI_NULLS ON\r\nGO\r\nSET QUOTED_IDENTIFIER ON\r\nGO\r\n-- =============================================\r\n-- Author:\t\tAndrew Pallant\r\n-- Create date: August 27, 2013\r\n-- Description:\tGet Numeric Value Prior to a String within a String.\r\n-- =============================================\r\nCREATE FUNCTION f_read_num_before_some_string \r\n(\r\n\t-- Add the parameters for the function here\r\n\t@stringValue varchar(max),\r\n\t@search varchar(max)\r\n)\r\nRETURNS varchar(100)\r\nAS\r\nBEGIN\r\n\t-- Declare the return variable here\r\nDECLARE @intval as varchar(100)   -- Value to be returned\r\nDECLARE @RECORDINGVALUE bit       -- Flag to indicate the recording of the value to be returned\r\ndeclare @newvalue as varchar(100) -- Working String Value\r\n\r\n-- Set Defaults\r\nset @newvalue = ''                \r\nset @RECORDINGVALUE = 0\r\nset @intval = null\r\n\r\n-- Start Looping and Building\t\r\n\tIF len(isnull(@stringValue,'')) &gt; 0 BEGIN\r\n\t\tdeclare @startIndex as int\r\n\t\tset @startIndex = CHARINDEX(@search,@stringValue)\r\n\r\n\t\tdeclare @counter int\r\n\t\tset @counter = @startIndex\r\n\r\n\t\tWHILE @counter &gt; 0 BEGIN\r\n\t\t\tdeclare @temp as varchar(1)\r\n\t\t\tset @temp = substring(@stringValue,@counter,1)\r\n\t\t\tif @temp = ' ' and LEN(@newvalue)&gt; 0 BEGIN\r\n\t\t\t\t-- STOP - Drop and Roll\r\n\t\t\t\tset @counter = -1\r\n\t\t\tEND \r\n\r\n\t\t\t-- If we have a space - do work!\r\n\t\t\tIF @temp &lt;&gt; ' ' BEGIN\r\n\t\t\t\tIF @RECORDINGVALUE = 1 and isnumeric(@temp) = 0 BEGIN\r\n\t\t\t\t\tset @counter = -1\r\n\t\t\t\t\t-- STOP - Drop and Roll\r\n\t\t\t\tEND ELSE IF isnumeric(@temp) = 1 BEGIN\r\n\t\t\t\t\tset @RECORDINGVALUE = 1\r\n\r\n\t\t\t\t\t-- Build Return Value\r\n\t\t\t\t\tset @newvalue = @temp + @newvalue;\r\n\t\t\t\tEND\r\n\t\t\tEND \r\n\r\n\t\t\tset @counter = @counter - 1\r\n\t\tEND\r\n\tEND\r\n\r\n\tIF @newvalue = '' BEGIN SET @newvalue = '0' END\r\n\r\n\t-- Return the result of the function\r\n\tRETURN @newvalue\r\nEND\r\nGO<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently I had a need to extract the number from a string in the database. \u00a0The string would be something like &#8216;Monitor 16&#8243;&#8216; or &#8220;16 inch Monitor&#8221;. \u00a0 I would need to get the size for various reasons including fees and reports, but no real good way of doing it. \u00a0I have come up with &hellip; <a href=\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to use SQL to Extract a Number from a String<\/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":[141,36,78],"tags":[165,246],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>How to use SQL to Extract a Number from a String - LDNDeveloper<\/title>\r\n<meta name=\"description\" content=\"Use SQL to extract a random numeric from a string based on keywords\" \/>\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=\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"How to use SQL to Extract a Number from a String - LDNDeveloper\" \/>\r\n<meta property=\"og:description\" content=\"Use SQL to extract a random numeric from a string based on keywords\" \/>\r\n<meta property=\"og:url\" content=\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\" \/>\r\n<meta property=\"og:site_name\" content=\"LDNDeveloper\" \/>\r\n<meta property=\"article:published_time\" content=\"2013-08-27T16:57:07+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2013-11-26T16:42:19+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=\"2 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\"},\"author\":{\"name\":\"ldnDeveloper\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"headline\":\"How to use SQL to Extract a Number from a String\",\"datePublished\":\"2013-08-27T16:57:07+00:00\",\"dateModified\":\"2013-11-26T16:42:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\"},\"wordCount\":73,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"keywords\":[\"Numbers\",\"SQL\"],\"articleSection\":[\"Developement\",\"How To\",\"SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\",\"url\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\",\"name\":\"How to use SQL to Extract a Number from a String - LDNDeveloper\",\"isPartOf\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#website\"},\"datePublished\":\"2013-08-27T16:57:07+00:00\",\"dateModified\":\"2013-11-26T16:42:19+00:00\",\"description\":\"Use SQL to extract a random numeric from a string based on keywords\",\"breadcrumb\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/andrewpallant.ca\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use SQL to Extract a Number from a String\"}]},{\"@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":"How to use SQL to Extract a Number from a String - LDNDeveloper","description":"Use SQL to extract a random numeric from a string based on keywords","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":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/","og_locale":"en_US","og_type":"article","og_title":"How to use SQL to Extract a Number from a String - LDNDeveloper","og_description":"Use SQL to extract a random numeric from a string based on keywords","og_url":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/","og_site_name":"LDNDeveloper","article_published_time":"2013-08-27T16:57:07+00:00","article_modified_time":"2013-11-26T16:42:19+00:00","author":"ldnDeveloper","twitter_card":"summary_large_image","twitter_creator":"@LdnDeveloper","twitter_site":"@LdnDeveloper","twitter_misc":{"Written by":"ldnDeveloper","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#article","isPartOf":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/"},"author":{"name":"ldnDeveloper","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"headline":"How to use SQL to Extract a Number from a String","datePublished":"2013-08-27T16:57:07+00:00","dateModified":"2013-11-26T16:42:19+00:00","mainEntityOfPage":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/"},"wordCount":73,"commentCount":0,"publisher":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"keywords":["Numbers","SQL"],"articleSection":["Developement","How To","SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/","url":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/","name":"How to use SQL to Extract a Number from a String - LDNDeveloper","isPartOf":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#website"},"datePublished":"2013-08-27T16:57:07+00:00","dateModified":"2013-11-26T16:42:19+00:00","description":"Use SQL to extract a random numeric from a string based on keywords","breadcrumb":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-extract-number\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/andrewpallant.ca\/wordpress\/"},{"@type":"ListItem","position":2,"name":"How to use SQL to Extract a Number from a String"}]},{"@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\/1010"}],"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=1010"}],"version-history":[{"count":2,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1012,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1010\/revisions\/1012"}],"wp:attachment":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}