有个字段是序号,如1.2.1 1.2.2 1.2.3  ...  1.2.10 1.2.11,如果用order by时,则排序的结果为1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 ...
怎样让排序的结果按照1.2.1 1.2.2 1.2.3  ...  1.2.10 1.2.11这样排序?

解决方案 »

  1.   

    select * from tb order by cast(parsename(col,4) as int),cast(parsename(col,3) as int)
      

  2.   

    create table tb(col varchar(10))
    insert into tb values('1.2.1')
    insert into tb values('1.2.2') 
    insert into tb values('1.2.3')
    insert into tb values('1.2.10')
    insert into tb values('1.2.11')
    goselect * from tb 
    order by cast(parsename(col,4) as int),cast(parsename(col,3) as int),cast(parsename(col,2) as int)drop table tb/*
    col        
    ---------- 
    1.2.1
    1.2.2
    1.2.3
    1.2.10
    1.2.11(所影响的行数为 5 行)*/
      

  3.   

    select * from tb order by cast(parsename(col,4) as int),cast(parsename(col,3) as int),cast(parsename(col,2) as int)
      

  4.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([col] varchar(10))
    insert [tb]
    select '1.2.1' union all
    select '1.2.2' union all
    select '1.2.3' union all
    select '1.2.10' union all
    select '1.2.11'
    go-->测试开始
    select * from [tb] order by replace([col],'-','')/*
    col
    ----------
    1.2.1
    1.2.10
    1.2.11
    1.2.2
    1.2.3
    */
      

  5.   


    select * from tb order by cast(parsename(col,3) as int),cast(parsename(col,2) as int),cast(parsename(col,1) as int)
      

  6.   

    create table tb(col varchar(10))
    insert into tb values('1.2.1')
    insert into tb values('1.2.2') 
    insert into tb values('2.2.3')
    insert into tb values('1.2.10')
    insert into tb values('2.1.11')
    go
      select * from tb 
      order by cast(parsename(col,3) as int),
      cast(parsename(col,2) as int),
      cast(parsename(col,1) as int)
      
      /*
      col
      --------
      1.2.1
      1.2.2
      1.2.10
      2.1.11
      2.2.3
      
      */
      

  7.   

    select * from #tb 
    order by SUBSTRING(col,1,1),SUBSTRING(col,3,2)col
    ----------
    1.2.1
    1.2.2
    1.2.3
    1.2.10
    1.2.11(5 row(s) affected)
      

  8.   

    如果序号的位数是不确定的呢,可以用parsename吗?比如
    1.1
    1.2
    1.2.1
    1.2.2
    1.2.10
    1.2.10.1
      

  9.   

    create table tb(col varchar(10))
    insert into tb values('1.1')
    insert into tb values('1.2') 
    insert into tb values('1.2.1')
    insert into tb values('1.2.2')
    insert into tb values('1.2.10')
    insert into tb values('1.2.10.1')
    goselect * from tb 
    order by cast(parsename(col,4) as int),cast(parsename(col,3) as int),cast(parsename(col,2) as int),cast(parsename(col,1) as int)drop table tb/*
    col        
    ---------- 
    1.1
    1.2
    1.2.1
    1.2.2
    1.2.10
    1.2.10.1(所影响的行数为 6 行)
    */
    注意,最多三个'.',超过了则不行.
      

  10.   

    PARSENAME
    返回对象名的指定部分。可以检索的对象部分有对象名、所有者名称、数据库名称和服务器名称。说明  PARSENAME 函数不表明所指定名称的对象是否存在,而只是返回给定对象名的指定部分。
    语法
    PARSENAME ( 'object_name' , object_piece ) 参数
    'object_name'要检索其指定部分的对象名。object_name 是 sysname 值。本参数是可选的合法对象名。如果该对象名的所有部分均符合要求,则该名称由以下四部分组成:服务器名称、数据库名称、所有者名称和对象名。object_piece要返回的对象部分。object_piece 是 int 值,可以为下列值。Value 描述 
    1 对象名 
    2 所有者名称 
    3 数据库名称 
    4 服务器名称 
    返回类型
    nchar注释
    如果符合下列条件之一,则 PARSENAME 返回 NULL 值: object_name 或 object_piece 为 NULL 值。
    发生语法错误。
    所请求的对象部分长度为 0,并且是无效的 Microsoft® SQL Server™ 标识符。零长度的对象名将导致整个合法名称无效。 
    示例
    本示例使用 PARSENAME 返回有关 pubs 数据库中 authors 表的信息。USE pubs
    SELECT PARSENAME('pubs..authors', 1) AS 'Object Name'
    SELECT PARSENAME('pubs..authors', 2) AS 'Owner Name'
    SELECT PARSENAME('pubs..authors', 3) AS 'Database Name'
    SELECT PARSENAME('pubs..authors', 4) AS 'Server Name'下面是结果集:Object Name                    
    ------------------------------ 
    authors                        (1 row(s) affected)Owner Name                     
    ------------------------------ 
    (null)                         (1 row(s) affected)Database Name                  
    ------------------------------ 
    pubs                           (1 row(s) affected)Server Name                    
    ------------------------------ 
    (null)                         (1 row(s) affected)注意,最多三个'.',超过了则不行.得需要用函数了.
      

  11.   

    如果内容超过四段,大致需要如下的函数,仅供参考.
    create FUNCTION dbo.splitString
    (
        @string VARCHAR(MAX),
        @delimiter CHAR(1),
        @rep int =1
    )
    RETURNS varchar(10)
    BEGIN    DECLARE @start INT, @end INT
        SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
        declare @ic int
        set @ic = 1
        WHILE @start < LEN(@string) + 1 BEGIN
            if(@ic = @rep) return substring(@string, @start, @end-@start)
            IF @end = 0 
                SET @end = LEN(@string) + 1        SET @start = @end + 1
            SET @end = CHARINDEX(@delimiter, @string, @start)
            set @ic = @ic + 1
        END    return null
    END
    create table ta(col varchar(30))
    insert into ta values('[ad][ba][daf]') 
    insert into ta values('[erf][afda][dasfasdf]') 
    insert into ta values('[fas][fase][reb]') select dbo.SplitString(col, ']', 1)+']' as col1,
    dbo.SplitString(col, ']', 2)+']' as col2,
    dbo.SplitString(col, ']', 3)+']' as col3
    from tacol1                 col2                 col3 
    -----------   -----------   ----------- 
    [ad]                 [ba]                 [daf] 
    [erf]               [afda]             [dasfasdf] 
    [fas]               [fase]             [reb] 
      

  12.   

    create table tb(col varchar(10))
    insert into tb values('1.1')
    insert into tb values('1.2') 
    insert into tb values('1.2.1')
    insert into tb values('1.2.2')
    insert into tb values('1.2.10')
    insert into tb values('1.2.10.1')
    go
     select * ,replace(col,'.','') as 变换后 from tb order by cast(replace(col,'.','') as int)
    --呵呵,转成整形 再比较!
    col        变换后
    ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1.1        11
    1.2        12
    1.2.1      121
    1.2.2      122
    1.2.10     1210
    1.2.10.1   12101(6 行受影响)
      

  13.   

    1.1.1.1.1.1.1
    1.1.1.1.1.1.10
    1.1.1.1.1.1.12
    1.1.1.1.1.1.3
    1.1.1.1.1.1.2
    1.1.1.1.1.1.21
    感谢几位大侠的回答
    我用了Order by Cast(replace(Col,'.','') as int)就满足我的要求
    结果:
    1.1.1.1.1.1.1
    1.1.1.1.1.1.2
    1.1.1.1.1.1.3
    1.1.1.1.1.1.10
    1.1.1.1.1.1.12
    1.1.1.1.1.1.21