在SQL Server中按字母和字符排序

时间:2013-03-22 09:35:12

标签: sql sql-server sorting sql-order-by

运行我的SQL查询后,结果是

1.12.1
1.12.2
1.12.3
1.12.4
1.12.5
1.12.6
1.12.7
1.12.8
1.12.9
1.12.10
1.12.11
1.12.2a 

但我想要这个结果(“2”之后的“2a”)

1.12.1
1.12.2
1.12.2a <-- This one must be here, not at the end of the result set
1.12.3
1.12.4
1.12.5
1.12.6
1.12.7
1.12.8
1.12.9
1.12.10
1.12.11

什么样的SQL查询可以做到这一点?

以下是我的查询...我使用SQL Server 2012

  

sResultSqlBuilder.Append(“ORDER BY {0} {1}”)

     

如果sortExpression =“”那么

     

sResultSql = String.Format(sResultSql,“Right('0000000000'+ FirstNumber,   10),右('0000000000'+ SecondNumber,10),右('0000000000'+ ThirdNumbersAndLetter,10)   “,”“)

1 个答案:

答案 0 :(得分:0)

请尝试,它在MS-Sql服务器中运行:

SELECT VersionNo, 
Cast(PARSENAME(VersionNo, 3)as int) as Num1, 
cast(PARSENAME(VersionNo, 2) as int) as Num2, 
cast(PARSENAME(VersionNo, 1) as nvarchar(2)) as Num3 
FROM 
(
    select '1.12.2' as VersionNo union
    select '1.12.3' as VersionNo union
    select '1.12.2a' as VersionNo union
    select '1.12.1' as VersionNo union
    select '1.12.4' as VersionNo
)x
Order by Num1, Num2, num3