0

我有一张桌子,我想列joint_no 列。该列的值是这样的

FW-1 
FW-2
.
.
.
FW-13
FW-R1
FW-1A

当我订购它们时,我得到了这个结果

FW-1
FW-10
FW-11
FW-12
FW-13
FW-1A
.
.
FW-R1

我想在 sql 查询后得到这个结果

FW-1
FW-1A
FW-2
FW-3
..
FW-13
FW-R1

有谁能够帮我?

4

2 回答 2

2

如果你能做到,我建议你重新编号这些值,以便“逻辑”顺序遵循字母顺序。F-1 随后将更新为 F-01 或 F-001。

如果您不能这样做,请添加一个字段,该字段将填充您的代码的“有序”形式。然后,您可以按 F-001 列进行排序,但仍会显示 F-1 值

否则,订购您的记录将很快成为您的噩梦。

于 2013-09-28T12:17:46.793 回答
0

使用 Patindex 查找第一个数字表达式作为第一个排序字段,然后将数字部分提取为整数作为第二个排序字段,并使用整个字符串作为第三个排序字段,您可能会得到所需的结果。

Declare @a Table (c varchar(50))
Insert Into @a
Select 'FW-1'
Union Select 'FW-10'
Union Select 'FW-11'
Union Select 'FW-12'
Union Select 'FW-13'
Union Select 'FW-1A'
Union Select 'FW-2'
Union Select 'FW-3'
Union Select 'FW-R1'
Union Select 'FW-A1'
;With CTE as
(Select 1 as ID
Union All
Select ID + 1 from CTE where ID < 100
)

Select * from
(
Select c
 ,PATINDEX('%[0-9]%',c) as s1
 ,(Select Cast(
          (Select Case
                   When SUBSTRING(c, ID, 1) LIKE '[0-9]'
                   Then SUBSTRING(c, ID, 1)
                   Else ''
                  End
          From (Select * from CTE) AS X(ID)
          Where ID <= LEN(c)
          For XML PATH('')) 
          as int)
   )
  as s2
 from
@a
) x
order by
s1,s2,c

输出:

FW-1    4   1   -1
FW-1A   4   1   -1A
FW-2    4   2   -2
FW-3    4   3   -3
FW-10   4   10  -10
FW-11   4   11  -11
FW-12   4   12  -12
FW-13   4   13  -13
FW-A1   5   1   A1
FW-R1   5   1   R1

如果前导部分不固定(FW-),您可能需要添加一个额外的排序字段

Declare @a Table (c varchar(50))
Insert Into @a
Select 'FW-1'
Union Select 'FW-10'
Union Select 'FW-11'
Union Select 'FW-12'
Union Select 'FW-13'
Union Select 'FW-1A'
Union Select 'FW-2'
Union Select 'FW-3'
Union Select 'FW-R1'
Union Select 'FW-A1'
Union Select 'AB-A1'
Union Select 'AB-11'
;With CTE as
(Select 1 as ID
Union All
Select ID + 1 from CTE where ID < 100
)

Select * from
(
Select c
 ,SubString(c,1,PATINDEX('%[0-9]%',c)-1) as S0
 ,PATINDEX('%[0-9]%',c) as s1
 ,(Select Cast(
          (Select Case
                   When SUBSTRING(c, ID, 1) LIKE '[0-9]'
                   Then SUBSTRING(c, ID, 1)
                   Else ''
                  End
          From (Select * from CTE) AS X(ID)
          Where ID <= LEN(c)
          For XML PATH('')) 
          as int)
   )
  as s2
 from
@a
) x
order by
s0,s1,s2,c
于 2013-09-28T11:57:46.970 回答