Question: What datatype should I choose for storing an IP Address in a SQL Server?

By selecting the right datatype would it be easy enough to filter by IP address then?

Answer: 
`sys.dm_exec_connections` uses varchar(48) after SQL Server 2005 SP1. Sounds good enough for me especially if you want to use it compare to your value.

Realistically, you won't see IPv6 as mainstream for a while yet, so I'd prefer the 4 tinyint route. Saying that, I'm using varchar(48) because I have to use `sys.dm_exec_connections`...

Otherwise. Mark Redman's answer mentions a previous SO debate question.
I like the functions of SandRock. But I found an error in the code of **dbo.fn\_ConvertIpAddressToBinary**. The incoming parameter of @ipAddress VARCHAR(39) is too small when you concat the @delim to it.

```
SET @ipAddress = @ipAddress + @delim

```

You can increase it to 40. Or better yet use a new variable that is bigger and use that internally. That way you don't lose the last pair on large numbers.

```
SELECT dbo.fn_ConvertIpAddressToBinary('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')

```