1

表格1

id value
---------    
 1  100
2A  200
 2  300
10  500
 8  200
....

Select * 
from table1 
order by id

显示输出为

id value
------------    
 1 100
10 500
2A 200
 2 300
 8 200
....

如何正确下单?

预期产出

id value
----------
 1 100
 2 300
2A 200
 8 200
10 500
....
4

5 回答 5

1

如果固定最后一个字符可能是字符,那么您可以尝试以下查询

 WITH A(ID) 
 AS
 (
 SELECT '1'
 UNION ALL
 SELECT '2C'
 UNION ALL
 SELECT '2A'
 UNION ALL
 SELECT '2'
 UNION ALL
 SELECT '10'
 )
 SELECT * 
 FROM A
 ORDER BY 
 convert(int,
 Case When IsNumeric(ID) = 0 then left(ID,len(id)-1) 
 Else ID END
 ) , Case When IsNumeric(ID) = 0 then RIGHT(ID,1) Else '0' END

如果它是可变的,那么您可以编写一个函数,将 charecter 替换为其 ansi 值或 0 。然后在该列上关闭订单。

于 2012-10-28T19:16:11.480 回答
0
SELECT
LEFT(ID,1),
RIGHT(ID,1),
* 
FROM table1 
ORDER BY LEFT(ID,1),RIGHT(ID,1)

应该可以解决问题,我什至不确定所选语句中是否需要左右。

于 2012-10-28T18:33:35.570 回答
0
Select * 
from table1 
order by cast(replace(lower(id), 'abcdefg', '') as int),
         replace(id, '0123456789','');
于 2012-10-28T18:36:20.973 回答
0
SELECT * FROM table1 ORDER BY CAST(id as varchar(50))
于 2012-10-28T18:42:30.573 回答
0
CREATE FUNCTION [dbo].[RemoveNonAlphaCharacters](@Temp varchar(1000))
RETURNS int
AS
BEGIN
  WHILE PatIndex ('%[^0-9]%', @Temp) > 0
  SET @Temp = Stuff( @Temp, PatIndex('%[^0-9]%' , @Temp ), 1, '')
  RETURN @Temp
END

SELECT id, value
FROM dbo.Table1
ORDER BY [dbo].[RemoveNonAlphaCharacters](id) ASC
于 2012-10-29T12:03:27.223 回答