T-Sql - 按字母数字排序
我有一个字母数字标记列表,例如 '1a'、'1b'、'02'、'03'、'10'、'11'、
等...
现在,最好的方法是什么在此令牌列表上进行排序?
我收到 '1a', '1b', '10', '11', '02', '03',
但我需要它
'1a', '1b', '02', '03', '10', '11'
更新
ok,我在建议后这样做,但它不起作用。
declare @tokens table(token varchar(20));
insert into @tokens
select '1a'
select '1b'
select '02'
select '10'
select * from @tokens
order by case
when ISNUMERIC(token) = 1 then right('0000000000'+token+'0',10)
else right('0000000000'+token,10)
end
我收到的响应为 '1b', '02', '10', '1a'
UPDATE2
它在进行以下更改后有效。
declare @tokens table(token varchar(20));
insert into @tokens
select '1a'
insert into @tokens
select '1b'
insert into @tokens
select '02'
insert into @tokens
select '10'
select token from @tokens
order by case
when ISNUMERIC(token) = 1 then right('0000000000'+token+'0',10)
else right('0000000000'+token,10)
end
感谢大家的好主意。
i have a list of alphanumeric tokens, say '1a', '1b', '02', '03', '10', '11',
etc...
Now, what's the best way to do an order by on this list of tokens?
I am getting '1a', '1b', '10', '11', '02', '03',
but i need it to be
'1a', '1b', '02', '03', '10', '11'
UPDATE
ok, i am doing this after the suggestion but it's not working.
declare @tokens table(token varchar(20));
insert into @tokens
select '1a'
select '1b'
select '02'
select '10'
select * from @tokens
order by case
when ISNUMERIC(token) = 1 then right('0000000000'+token+'0',10)
else right('0000000000'+token,10)
end
I am getting the response as '1b', '02', '10', '1a'
UPDATE2
It works after making the following change.
declare @tokens table(token varchar(20));
insert into @tokens
select '1a'
insert into @tokens
select '1b'
insert into @tokens
select '02'
insert into @tokens
select '10'
select token from @tokens
order by case
when ISNUMERIC(token) = 1 then right('0000000000'+token+'0',10)
else right('0000000000'+token,10)
end
Thanks to all of you for your nice ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的解决方案是预先添加零,
但是,这不会考虑字母字符。为了处理字母字符,您需要知道可能有多少个潜在的字母字符。如果有的话,您可以执行以下操作:
添加
测试运行:
关于我的解决方案的注释。如果字母字符具有特殊含义,那么正如 Erick Robertson 建议的那样,您应该将数据分解为单独的列。上述解决方案仅处理两种非常特殊的情况:全数字值、带有单个尾随字母字符的值。如果数据可能包含多个字母字符,或者字母字符有时位于值末尾以外的位置,则我的解决方案将不起作用。此外,应该注意的是,我的解决方案将导致表扫描来评估每个值的可排序字符串。
如果您寻求的是一次性快速解决方案,那么我的方法将会起作用。如果您正在寻求长期解决方案,那么要么将数据分解为单独的列,接受愚蠢的排序顺序,要么添加一个列来规定每个值的相对排序顺序。
The easiest solution is to pre-pend zeros
However, this will not account for alpha characters. In order to deal with alpha characters, then you'd need to know how many potential alpha characters you might have. If there is one, you could do something like:
ADDITION
Test run:
A note about my solution. If it is the case that the alphabetic character(s) has special meaning, then as Erick Robertson suggested, you should break up the data into separate columns. The above solution will only handle two very specific cases: an all numeric value, a value with a single trailing alphabetic character. If the data might have multiple alphabetic characters or the alphabetic character is sometimes positioned other than the end of the value, my solution will not work. In addition, it should be noted that my solution will cause a table scan to evaluate the order-able string on each value.
If what you seek is a one-time quick solution, then my approach will work. If you are seeking a long term solution, then either break up the data into separate columns, accept the goofy sort order or add a column that dictates the relative sort order for each value.
如果您熟悉 C# 或 VB.net,可能值得考虑编写一个 CLR 函数来为您执行排序,因为这种排序顺序非常不标准,很难在 TSQL 中全面、正确地描述。
If you're familiar with C# or VB.net, it might be worth considering writing a CLR function that performs the sorting for you as this sort order is non-standard enough to be quite hard to describe comprehensively and correctly in TSQL.
最好的解决方案是有一个单独的字段来存储令牌的 int 值。当您维护 token 列时,您应该维护此列。然后,当您排序时,先按 int 值列排序,然后按 token 列排序。这将允许您为这些列建立索引,以便快速检索大型数据集的数据。
从 alpha 到 int 的转换函数速度很慢,并且无法利用索引来加速查询。随着数据集的增长,这种类型的解决方案只会变得更慢,并使数据库陷入困境。
The best solution is to have a separate field which stores the int value of the token. You should maintain this column when you maintain the token column. Then when you sort, order by the int value column then the token column. This will allow you to index these columns for fast retrieval of data with large data sets.
Conversion functions from alpha to int are slow and cannot take advantage of indexing to speed up queries. As your data set grows, this type of solution will only get slower and bog down your database.