Created
July 17, 2018 17:21
-
-
Save h3nryza/a752039a485395023a024484055a634c to your computer and use it in GitHub Desktop.
SQL Return ip address from integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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