简体   繁体   中英

SQL order by included character and string

I have a table and i want to colum joint_no column. The column's values are like these

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

When i ordered them i get this results

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

I want to get this result after sql query

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

can anybody help me?

If you can do it, I'd advise you to renumber the values so that the 'logical' order sticks to the alphabetical order. F-1 will then be updated to F-01, or F-001.

If you cannot do it, add a field that will be populated with the 'ordered' form of your code. You 'll then be able to order by the F-001 column and still display the F-1 value

Otherwise ordering your records will rapidly become your nightmare.

Using Patindex to find the first numeric expression as first sort field, then extracting the numeric part as integer as second sortfield and using the whole string as third sort field you might get the desired result.

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

With the output:

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

If the leading part is not fixed (FW-) you might need to add one additional sort field

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM