Skip to content

Instantly share code, notes, and snippets.

@JamesTryand
Created December 20, 2013 11:28
Show Gist options
  • Select an option

  • Save JamesTryand/8053548 to your computer and use it in GitHub Desktop.

Select an option

Save JamesTryand/8053548 to your computer and use it in GitHub Desktop.
MSSql Scalar Function that converts an IPAddress to its Numerical equivalent.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: James Tryand
-- Create date: 2013-12-20
-- Description: Converts An IPAddress to it's numerical equivalent
-- =============================================
CREATE FUNCTION IPAddress2IPNumber
(
@IPaddress varchar(20)
)
RETURNS bigint
AS
BEGIN
RETURN
16777216 * CAST(ParseName(@IPaddress, 1) as bigint) +
65536 * CAST(ParseName(@IPaddress, 2) as bigint) +
256 * CAST(ParseName(@IPaddress, 3) as bigint) +
CAST(ParseName(@IPaddress, 4) as bigint)
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment