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----

Comments

Popular posts from this blog

SMS "Program failed (run time exceeded)" 10070

SMS "Waiting for User Condition" 10036

SMS "Waiting for content" 10035