Sorting would be easier if you store each octet in separate columns.
SQL sort varchar
Handian Sudianto
6,541
Reputation points
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,
SQL Server | Other
Answer accepted by question author
1 additional answer
Sort by: Most helpful
-
Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator2025-01-31T22:49:25.08+00:00 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))