0

嗨,我在名为“ NumberName ”的字段名称中有这样的值

AB1
CD2
XH506
PQ104
PZ77

到目前为止,我尝试订购的是:

select * from view_name where NumberName='Something' 
order by RIGHT('0000' + SUBSTRING(NumberName, ISNULL(NULLIF(PATINDEX('%[0-9]%',NumberName), 0), 
LEN(NumberName)+1), LEN(NumberName)), 4)

order by LEN(NumberName),NumberName

如何实现这一目标..?

4

3 回答 3

1

也许,我没有完全理解你的问题。但ORDER BY NumberName正确地对值进行排序。

询问:

DECLARE @temp TABLE(NumberName NVARCHAR(50))

INSERT INTO @temp (NumberName)
VALUES ('AB1'),('CD2'),('XH506'),('PQ104'),('PZ77')

SELECT *
FROM @temp
ORDER BY NumberName

输出:

NumberName
----------
AB1
CD2
PQ104
PZ77
XH506
于 2013-05-27T09:23:53.973 回答
1
select * from yourtable
order by LEFT(columnname,1)
于 2013-05-27T09:00:36.130 回答
0

你没有很好地解释这一点,但我认为你想根据最右边数字的值对结果进行排序。尝试这个。您需要首先找到第一次出现的非数字形式,以了解最后一个数字的开始位置(PATINDEX('%[0-9]%会给您第一个数字,但在“FB3C2”中,例如数字与字母混合)。

select 
    * 
    ,right(NumberName,PATINDEX('%[A-Z]%',reverse(NumberName))-1)
from (
    select 'ABC001' as numbername union all
    select 'XQ20001' union all
    select 'XQ20002' union all
    select 'XQ20003' union all
    select 'XQ20004' union all
    select 'PM130010' union all
    select 'PM130011' union all
    select 'PM130012' union all
    select 'PM130013' union all
    select 'PM130014' union all
    select 'PM130015' union all
    select 'FB3C2' union all
    select 'FB3C2' union all
    select 'PM13001' union all
    select 'PM13001' union all
    select 'PM13002'
) x
order by 
    cast(right(NumberName,PATINDEX('%[A-Z]%',reverse(NumberName))-1) as int)
于 2013-05-27T09:07:46.107 回答