SQL Function to take an IP and return the class B

Creates a SQL function to take an IP and return the class B. There's probably a better way to do this, if you've got one, I'd love to see it. Comment out the second "Set @returnIp" to return a Class C.

----Begin SQL----
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE FUNCTION dbo.getClassB(@Ip varchar(16))

RETURNS varchar(16) AS
BEGIN

Declare @returnIp varchar(16)

Set @returnIp = Reverse(Right(Reverse(@IP),Len(Reverse(@IP)) - Charindex('.',Reverse(@IP))))
Set @returnIp = Reverse(Right(Reverse(@returnIp),Len(Reverse(@returnIp)) - Charindex('.',Reverse(@returnIp))))

return @returnIp
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
----End SQL----

About the Author

Emmanuel Tsouris is a Systems Management veteran and Developer Advocate specializing in PowerShell and Cloud Automation. He maintains DotVBS to preserve legacy knowledge for the "archaeologist admin."

Ready to move from VBScript to the Cloud? Check out his book, Pro PowerShell for Amazon Web Services.

Visit EmmanuelTsouris.com for his latest projects.

As an Amazon Associate, I earn from qualifying purchases at no cost to you.

Comments

Popular posts from this blog

SMS "Program failed (run time exceeded)" 10070

SMS "Waiting for User Condition" 10036

Reading the SCCM Client GUID