Skip to content

Instantly share code, notes, and snippets.

@h3nryza
Created July 17, 2018 17:21
Show Gist options
  • Save h3nryza/a752039a485395023a024484055a634c to your computer and use it in GitHub Desktop.
Save h3nryza/a752039a485395023a024484055a634c to your computer and use it in GitHub Desktop.
SQL Return ip address from integer
CREATE FUNCTION dbo.IntegerToIPAddress (@IP AS bigint)
RETURNS varchar(15)
AS
BEGIN
DECLARE @Octet1 tinyint
DECLARE @Octet2 tinyint
DECLARE @Octet3 tinyint
DECLARE @Octet4 tinyint
DECLARE @RestOfIP bigint
SET @Octet1 = @IP / 16777216
SET @RestOfIP = @IP - (@Octet1 * 16777216)
SET @Octet2 = @RestOfIP / 65536
SET @RestOfIP = @RestOfIP - (@Octet2 * 65536)
SET @Octet3 = @RestOfIP / 256
SET @Octet4 = @RestOfIP - (@Octet3 * 256)
RETURN(CONVERT(varchar, @Octet1) + '.' +
CONVERT(varchar, @Octet2) + '.' +
CONVERT(varchar, @Octet3) + '.' +
CONVERT(varchar, @Octet4))
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment