{"id":700,"date":"2012-01-17T10:04:15","date_gmt":"2012-01-17T08:04:15","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=700"},"modified":"2012-01-17T10:04:15","modified_gmt":"2012-01-17T08:04:15","slug":"ip-address-to-table-sql-function","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/01\/ip-address-to-table-sql-function\/","title":{"rendered":"IP address to table sql function"},"content":{"rendered":"<p>I have a requirement to do some stuff with a table that contains ip addresses like grouping per subnets etc. Doing this with straight tsql is tricky (if possible at all) so I created a sql server function that takes the ip address field and breaks it down to a set (table) of 4 values. It&#8217;s not perfect and brings another type of complexity of its own but at least you can compare the separate bits of the ip address as unique parts. Also, it doesn&#8217;t really have any error checking built in.<\/p>\n<blockquote><p>create FUNCTION [dbo].[ufn_IPAddressToTable]<br \/>\n(<\/p>\n<p style=\"padding-left: 30px;\">@IpAddress VARCHAR(15)<\/p>\n<p>)<br \/>\nRETURNS @IpTable TABLE (part1 int, part2 int, part3 int, part4 int)<br \/>\nAS<br \/>\nBEGIN<\/p>\n<p style=\"padding-left: 30px;\">DECLARE @part1 int, @part2 int, @part3 int, @part4 int<br \/>\nIF (not @IpAddress is null)<br \/>\nBEGIN<\/p>\n<p style=\"padding-left: 60px;\">if (CHARINDEX(&#8216;.&#8217;, @IpAddress) &gt; 0)<br \/>\nbegin<\/p>\n<p style=\"padding-left: 90px;\">set @part1 = CONVERT(int, substring(@IpAddress, 0, CHARINDEX(&#8216;.&#8217;, @IpAddress)))<br \/>\nset @IpAddress = substring(@IpAddress, CHARINDEX(&#8216;.&#8217;, @IpAddress) + 1, 100)<\/p>\n<p style=\"padding-left: 90px;\">if (CHARINDEX(&#8216;.&#8217;, @IpAddress) &gt; 0)<br \/>\nbegin<\/p>\n<p style=\"padding-left: 120px;\">set @part2 = CONVERT(int, substring(@IpAddress, 0, CHARINDEX(&#8216;.&#8217;, @IpAddress)))<br \/>\nset @IpAddress = substring(@IpAddress, CHARINDEX(&#8216;.&#8217;, @IpAddress) + 1, 100)<\/p>\n<p style=\"padding-left: 120px;\">if (CHARINDEX(&#8216;.&#8217;, @IpAddress) &gt; 0)<br \/>\nbegin<\/p>\n<p style=\"padding-left: 150px;\">set @part3 = CONVERT(int, substring(@IpAddress, 0, CHARINDEX(&#8216;.&#8217;, @IpAddress)))<br \/>\nset @IpAddress = substring(@IpAddress, CHARINDEX(&#8216;.&#8217;, @IpAddress) + 1, 100)<br \/>\nset @part4 = CONVERT(int, @IpAddress)<\/p>\n<p style=\"padding-left: 120px;\">end<\/p>\n<p style=\"padding-left: 90px;\">end<\/p>\n<p style=\"padding-left: 60px;\">end<br \/>\nif (not(@part1 is null or @part2 is null or @part3 is null or @part4 is null))<\/p>\n<p style=\"padding-left: 90px;\">insert @IpTable(part1, part2, part3, part4)<br \/>\nvalues (@part1, @part2, @part3, @part4)<\/p>\n<p style=\"padding-left: 30px;\">END<br \/>\nRETURN<\/p>\n<p>END<\/p><\/blockquote>\n<p>As an example you can use it like this:<\/p>\n<blockquote><p>declare @IpAddress varchar(15)<br \/>\nset @IpAddress = &#8216;127.0.0.1&#8217;<br \/>\nselect * from dbo.ufn_IPAddressToTable(@IpAddress)<\/p><\/blockquote>\n<p>Or if you have a table with ip addresses<\/p>\n<blockquote><p>with ComputerIps(Area, IpPart1, IpPart2, IpPart3)<br \/>\nas<br \/>\n(<\/p>\n<p style=\"padding-left: 30px;\">select\u00a0 c.Area,<br \/>\n(select top 1 part1 from dbo.[ufn_IPAddressToTable](c.IpAddress)) as Part1,<br \/>\n(select top 1 part2 from dbo.[ufn_IPAddressToTable](c.IpAddress)) as Part2,<br \/>\n(select top 1 part3 from dbo.[ufn_IPAddressToTable](c.IpAddress)) as Part3<br \/>\nfrom Computers c<br \/>\nwhere not (c.IpAddress is null)\u00a0 and LEN(c.IpAddress) &gt; 0<\/p>\n<p>)<br \/>\nselect Area, IpPart1, IpPart2, IpPart3, COUNT(*) as [Computers]<br \/>\nfrom ComputerIps<br \/>\ngroup by Area, IpPart1, IpPart2, IpPart3<br \/>\norder by Area, IpPart1, IpPart2, IpPart3<\/p><\/blockquote>\n<p>Another problem is that it may be slow when the source table becomes large. At least it helps a bit.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a requirement to do some stuff with a table that contains ip addresses like grouping per subnets etc. Doing this with straight tsql is tricky (if possible at all) so I created a sql server function that takes &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/01\/ip-address-to-table-sql-function\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,303,117,49],"class_list":["post-700","post","type-post","status-publish","format-standard","hentry","category-development","tag-code","tag-development","tag-function","tag-sql"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=700"}],"version-history":[{"count":6,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/700\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/700\/revisions\/706"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}