34,592
社区成员
发帖
与我相关
我的任务
分享
select * from IpInfo order by IpAddr asc
declare @IpInfo table(ip varchar(20))
insert into @IpInfo
select '192.168.80.11' union all
select '192.168.80.114' union all
select '192.168.80.15'
select * from @IpInfo order by cast(replace(ip,'.','')as bigint)
/*
ip
--------------------
192.168.80.11
192.168.80.15
192.168.80.114
(3 行受影响)
*/
if object_id('[tb]') is not null drop table [tb]
go
CREATE TABLE [tb]([ipaddr] VARCHAR(14))
INSERT [TB]
SELECT '192.168.80.11' UNION ALL
SELECT '192.168.80.114' UNION ALL
SELECT '192.168.80.15'
SELECT * FROM [TB]
ORDER BY CAST(right(IpAddr,len(IpAddr)-11) AS int)
ipaddr
--------------
192.168.80.11
192.168.80.15
192.168.80.114
(3 行受影响)
ORDER BY CAST(right(IpAddr,len(IpAddr)-11) AS int) --假定前面都一样192.168.80.
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ipaddr] varchar(14))
insert [tb]
select '192.168.80.11' union all
select '192.168.80.114' union all
select '192.168.80.15'
---查询---
select
*
from
[tb]
order by
cast(right(ipaddr,charindex('.',reverse(ipaddr))-1) as int)
---结果---
ipaddr
--------------
192.168.80.11
192.168.80.15
192.168.80.114
(所影响的行数为 3 行)
select *from
(
select '192.168.80.11' AS IpAddr union all
select '192.168.80.114' union all
select '192.168.80.15'
)T
order by cast(parsename(IpAddr,4) as int),cast(parsename(IpAddr,3) as int),cast(parsename(IpAddr,2) as int),cast(parsename(IpAddr,1) as int)
DECLARE @T TABLE(IP VARCHAR(50))
INSERT @T SELECT '192.168.80.11'
INSERT @T SELECT '192.168.80.114'
INSERT @T SELECT '192.168.80.15'
SELECT * FROM @T ORDER BY CAST(REVERSE(LEFT(REVERSE(IP),CHARINDEX('.',REVERSE(IP))-1))AS INT)
/*IP
--------------------------------------------------
192.168.80.11
192.168.80.15
192.168.80.114
*/
ORDER BY right(IpAddr,len(IpAddr)-11) --假定前面都一样192.168.80.
select * from IpInfo order by replace(@s,'192.168.80.','') asc
order by cast(right(ipaddr,charindex('.',reverse(ipaddr))-1) as int)