我使用的是SQL Server,该列是VARCHAR(50)
,我希望对其进行排序:
1A
1B
2
2
3
4A
4B
4C
5A
5B
5C
5N
14 Draft
21
22A
22B
23A
23B
23C
23D
23E
25
26
FR01584
MISC
到目前为止我所拥有的是:
Select *
From viewASD
ORDER BY
Case When IsNumeric(LEFT(asdNumNew,1)) = 1
Then CASE When IsNumeric(asdNumNew) = 1
Then Right(Replicate('0',20) + asdNumNew + '0', 20)
Else Right(Replicate('0',20) + asdNumNew, 20)
END
When IsNumeric(LEFT(asdNumNew,1)) = 0
Then Left(asdNumNew + Replicate('',21), 20)
End
但是这个SQL语句将' 14草案'在' 26'之后。
有人可以帮忙吗?感谢
答案 0 :(得分:3)
您的WHERE语句是......奇怪的复杂。
看起来您希望按整数顺序排序任何前导数字,然后按余数排序。如果是这样,你应该把它作为单独的条款,而不是试图一个一个。您遇到的具体问题是您只允许使用一位数字,而不是两位或更多。 (还有No such thing as two。)
这是您的修复,以及SQLFiddle,为您的ORDER BY使用两个单独的计算列测试。 (请注意,这假定asdNumNew
的数字部分适合T-SQL int
。如果不是,则需要调整CAST和第一个ELSE的最大值。)
SELECT * FROM viewASD
ORDER BY
CASE
WHEN ISNUMERIC(asdNumNew)=1
THEN CAST(asdNumNew as int)
WHEN PATINDEX('%[^0-9]%',asdNumNew) > 1
THEN CAST(
LEFT(
asdNumNew,
PATINDEX('%[^0-9]%',asdNumNew) - 1
) as int)
ELSE 2147483648
END,
CASE
WHEN ISNUMERIC(asdNumNew)=1
THEN NULL
WHEN PATINDEX('%[^0-9]%',asdNumNew) > 1
THEN SUBSTRING(
asdNumNew,
PATINDEX('%[^0-9]%',asdNumNew) ,
50
)
ELSE asdNumNew
END
答案 1 :(得分:0)
如果字符串中的所有数字都相当小,请说不超过 10位数, 您可以将字符串中的所有数字扩展为完全 10位数:
123A - > 0000000123A
S4 -> S0000000004
A3B89 - > A0000000003B0000000089
依此类推,然后对它们进行排序
-- Expand all numbers within S by zeros to be MaxLen
create function [dbo].ExpandNumbers(@S VarChar(4000), @maxlen integer) returns VarChar(4000)
as
begin
declare @result VarChar(4000);
declare @buffer VarChar(4000);
declare @Ch Char;
declare @i integer;
set @buffer = '';
set @result = '';
set @i = 1;
while (@i <= len(@S))
begin
set @Ch = substring(@S, @i, 1);
if ((@Ch >= '0') and (@Ch <= '9'))
set @buffer = @buffer + @Ch
else
begin
if (len(@buffer) > 0)
set @result = @result + right(replicate('0', @maxlen) + @buffer, @maxlen);
set @buffer = '';
set @result = @result + @Ch;
end;
set @i = @i + 1;
end;
if (len(@buffer) > 0)
set @result = @result + right(replicate('0', @maxlen) + @buffer, @maxlen);
return @result;
end;
-- Final query is
select *
from viewASD
order by [dbo].ExpandNumbers(asdNumNew)
答案 2 :(得分:0)
尝试这个
声明@t table(Number nvarchar(20))
插入@t
选择
&#39; L010&#39;
联盟所有选择
&#39; L011&#39;
联盟所有选择
&#39; L011&#39;
联盟所有选择
&#39; L001&#39;
联盟所有选择
&#39; L012&#39;
联盟所有选择
&#39; 18&#39;
联盟所有选择
&#39; 8&#39;
联盟所有选择
&#39; 17&#39;
union all select 'B004'
union all SELECT 'B006'
union all SELECT 'B008'
union all SELECT 'B018'
union all SELECT 'UG001'
union all SELECT 'UG011'
union all SELECT 'G001'
union all SELECT 'G002'
union all SELECT 'G011'
select * from @t
order by cast(SUBSTRING(Number, 1,
case when patindex('%[^0-9]%',Number) > 0 then patindex('%[^0-9]%',Number) - 1 else LEN(Number) end) as int), Number
<强> O / P 强>
**Number**
B004
B006
B008
B018
G001
G002
G011
L001
L010
L011
L011
L012
UG001
UG011
8
17
18
答案 3 :(得分:0)
我有类似的东西,但有破折号作为主角和尾随空格的可能性。这段代码对我有用。
SELECT
my_column,
PATINDEX('%[^0-9]%',my_column) AS first_alpha_position,
CONVERT(INT,
CASE
WHEN PATINDEX('%[^0-9]%',my_column) = 0 OR PATINDEX('-%',my_column) = 1
THEN ABS(my_column)
ELSE SUBSTRING(my_column,1,PATINDEX('%[^0-9]%',my_column) -1)
END) AS numeric_value,
LTRIM(
SUBSTRING(my_column,PATINDEX('%[^0-9]%',my_column),LEN(my_column)-PATINDEX('%[^0-9]%',my_column)+1)
) AS alpha_chars
FROM my_table
ORDER BY numeric_value,alpha_chars