create function getIP(@a varchar(15))
returns varchar(15)
As
begin
declare @s varchar(15)
set @s = ''
while charindex('.',@a) > 0
begin
set @s = @s + right('000' + left(@a,charindex('.',@a)),4)
set @a = right(@a,len(@a)-charindex('.',@a))
end
set @s = @s + right('000' + @a,3)
return @s
end/*
Select dbo.getIP('202.1.110.2')
--------------- 
202.001.110.002
*/select max(dbo.getIP(nodes)) from test

解决方案 »

  1.   

    create function getIP(@a varchar(15))
    returns varchar(15)
    As
    begin
    declare @s varchar(15)
    set @s = ''
    while charindex('.',@a) > 0
    begin
    set @s = @s + right('000' + left(@a,charindex('.',@a)),4)
    set @a = right(@a,len(@a)-charindex('.',@a))
    end
    set @s = @s + right('000' + @a,3)
    return @s
    end/*
    Select dbo.getIP('202.1.110.2')
    --------------- 
    202.001.110.002
    */
    创建测试环境:
    create table test(nodes varchar(20))
    insert test select '1.1.1.1'
    union all select '1.1.1.5'
    union all select '1.1.1.6'
    union all select '1.1.1.7'
    union all select '1.1.1.8'
    union all select '1.1.1.10'运行:
    Select nodes from test where dbo.getip(nodes) = 
      (select max(dbo.getIP(nodes)) from test)nodes                
    -------------------- 
    1.1.1.10(所影响的行数为 1 行)
      

  2.   

    字符串的大小比较问题,你可以这样写:
    select top 1 nodes from test order by len(nodes) desc