SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,432 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
i have table contain of ip address, how we can sort the ip address from .1 to .254?
If i sort by asc then the result is not sequence like below pic. After .1 then jump to .102 not to .2
The field using varchar,
Sorting would be easier if you store each octet in separate columns.
Here is a solution which I think is better than splitting the value into four:
SELECT ip_address
FROM tbl
ORDER BY convert(int, parsename(ip_address, 4)),
convert(int, parsename(ip_address, 3)),
convert(int, parsename(ip_address, 2)),
convert(int, parsename(ip_address, 1))