我是用SQL2005, IP地址保存为 nchar(16) 类型,不知道为什么,两位数的IP居然排的比三位数的IP还要后,用order by ip ASC
都无效,这是为什么啊??如果要解决这个问题,有什么方法。还有,IP地址除了CHAR类型还能储存为什么类型?? 我试过INT ,flaat 都不行。

解决方案 »

  1.   

    字符型,是按ASCII比较的
    123
    14
      

  2.   

    字符型,是按ASCII比较的 
    14
    123 
      

  3.   

    当然了,IP不会超过255,我只是举例子。IP保存可以用转化为整型。或者采用不够三位的,前边用0来补充。比如说,90用090保存,肯定排在前面了。
      

  4.   

    create table tb(ip varchar(15))
    insert into tb values('192.168.0.1')
    insert into tb values('92.68.10.1')
    insert into tb values('192.168.1.1')
    goselect * from tb
    order by 
    right('0'+parsename(ip,4),3),
    right('0'+parsename(ip,3),3),
    right('0'+parsename(ip,2),3),
    right('0'+parsename(ip,1),3)drop table tb/*
    ip              
    --------------- 
    92.68.10.1
    192.168.0.1
    192.168.1.1(所影响的行数为 3 行)
    */
      

  5.   

    用parsename()函数,真是太妙了,厉害
      

  6.   

    应该加两个0才对.create table tb(ip varchar(15)) 
    insert into tb values('192.168.0.1') 
    insert into tb values('92.68.10.1') 
    insert into tb values('192.168.1.1') 
    go select * from tb 
    order by 
    right('00'+parsename(ip,4),3), 
    right('00'+parsename(ip,3),3), 
    right('00'+parsename(ip,2),3), 
    right('00'+parsename(ip,1),3) drop table tb 
      

  7.   

    学到了parsename的用法了,呵呵