您好我的字段名称中包含这样的值,名为“ 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
如何实现这个目标..?
答案 0 :(得分:1)
select * from yourtable
order by LEFT(columnname,1)
答案 1 :(得分: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
答案 2 :(得分:0)
你没有足够好地解释这一点,但我想要你根据最右边数字的值对结果进行排序。试试这个。你需要首先找到右边第一次出现的非数字,才能知道最后一个数字的开始位置(PATINDEX('%[0-9]%
会给你第一个数字,但在&#34; FB3C2&#34;例如数字是混合字母)。
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)