CREATE FUNCTION dbo.tf_LocationByIP ( @IP varchar(15) ) RETURNS TABLE AS RETURN ( SELECT dbo.Locations.Locations_ID, dbo.Locations.Locations_Name, dbo.IPs.IPRange_Start, dbo.IPs.IPRange_End FROM dbo.IPs INNER JOIN dbo.Locations ON dbo.IPRanges.Locations_ID = dbo.Locations.Locations_ID WHERE CAST(PARSENAME(@IP, 2) AS INT) BETWEEN CAST(PARSENAME(dbo.IPs.IPRange_Start, 2) AS INT) AND CAST(PARSENAME(dbo.IPs.IPRange_End, 2) AS INT) ) ----------------------------------- Explanation: @IP is the variable being passed from the workstation. The SELECT and FROM statement tells SQL what to pull and from where. The Locations and IPs table are joined on a common column, Locations_ID. The WHERE clause: Pulls the third octet from the IP address variable and cast it as an integer (it was a number sent as a varchar). Pulls the third octet from the IP addresses located in the Start and End IP ranges coumns and cast it as an integer. Finds the data where the parsed variable IP is located between the parsed Start and End IP range octets.