Skip to content

Instantly share code, notes, and snippets.

@DvdKhl
Last active May 29, 2022 22:20
Show Gist options
  • Save DvdKhl/6d2a88b7814852867effdf9d8a558900 to your computer and use it in GitHub Desktop.
Save DvdKhl/6d2a88b7814852867effdf9d8a558900 to your computer and use it in GitHub Desktop.
SQL Server function to convert varbinary(16) IPv4/IPv6 addresses to its string (shortened) form
--Copyright (C) 2022 DvdKhl
--Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
--to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
--and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
--The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
--WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
create or alter function [BytesToIPAddress](@b varbinary(16)) returns varchar(39) as
begin
declare @str varchar(39) = ''
if len(@b) = 4
begin
set @str =
cast(cast(substring(@b, 1, 1) as tinyint) as varchar(3)) + '.' +
cast(cast(substring(@b, 2, 1) as tinyint) as varchar(3)) + '.' +
cast(cast(substring(@b, 3, 1) as tinyint) as varchar(3)) + '.' +
cast(cast(substring(@b, 4, 1) as tinyint) as varchar(3))
end else
begin
declare @i int = 0
declare @trimStartPos int = 0
declare @maxTrimStartPos int = 0
declare @maxTrimEndPos int = 0
declare @maxZeroSectionCount int = 0
declare @currentZeroSectionCount int = 0
while (@i < 16)
begin
declare @binPart varbinary(2) = substring(@b, @i + 1, 2)
declare @intPart int = cast(@binPart as int)
declare @partLength int;
if @intPart > 0x0FFF set @partLength = 4
else if @intPart > 0x00FF set @partLength = 3
else if @intPart > 0x000F set @partLength = 2
else set @partLength = 1
declare @part varchar(4) = right(convert(varchar(6), @binPart, 1), @partLength)
set @str = @str + @part
if @intPart = 0
begin
set @currentZeroSectionCount = @currentZeroSectionCount + 1
if @currentZeroSectionCount > @maxZeroSectionCount
begin
set @maxZeroSectionCount = @currentZeroSectionCount
set @maxTrimStartPos = @trimStartPos
set @maxTrimEndPos = len(@str)
end
end else
begin
set @currentZeroSectionCount = 0
set @trimStartPos = len(@str)
end
if @i < 14 set @str = @str + ':'
set @i = @i + 2
end
if @maxZeroSectionCount > 1
begin
set @str = substring(@str, 1, @maxTrimStartPos) + '::' + substring(@str, @maxTrimEndPos + 2, 39)
end
end
return @str
end
GO
--Satisfies https://datatracker.ietf.org/doc/html/rfc5952
select
dbo.[BytesToIPAddress](0x00000000), --0.0.0.0
dbo.[BytesToIPAddress](0x01010101), --1.1.1.1
dbo.[BytesToIPAddress](0xffffffff), --255.255.255.255
dbo.[BytesToIPAddress](0x00000000000000000000000000000000), --::
dbo.[BytesToIPAddress](0x11111111111111111111111111111111), --1111:1111:1111:1111:1111:1111:1111:1111
dbo.[BytesToIPAddress](0x00010001000100010001000100010001), --1:1:1:1:1:1:1:1
dbo.[BytesToIPAddress](0x00001111111111111111111111111111), --0:1111:1111:1111:1111:1111:1111:1111
dbo.[BytesToIPAddress](0x00000000111111111111111111111111), --::1111:1111:1111:1111:1111:1111
dbo.[BytesToIPAddress](0x11111111111111111111111111110000), --1111:1111:1111:1111:1111:1111:1111:0
dbo.[BytesToIPAddress](0x11111111111111111111111100000000), --1111:1111:1111:1111:1111:1111::
dbo.[BytesToIPAddress](0x11111111000011111111111111111111), --1111:1111:0:1111:1111:1111:1111:1111
dbo.[BytesToIPAddress](0x11111111000000001111111111111111), --1111:1111::1111:1111:1111:1111
dbo.[BytesToIPAddress](0x11110000000000001111000000000000), --1111::1111:0:0:0
dbo.[BytesToIPAddress](0x00000000000011110000000000000000) --0:0:0:1111::
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment