我有一个有书名字段的表格。我希望能够对这些记录进行排序:
等等,忽略“A”,“An”,&当它们作为标题的第一个单词出现时。 (在标题的任何地方也可以忽略它们。)
我知道这些是SQL Server 2008中的停用词,因此如果有人在搜索中使用它们,就可以忽略它们。
但有没有办法让ORDER BY忽略它们? (如果它有所不同,查询将在ASP.NET中使用LinqDataSource。)
谢谢!
答案 0 :(得分:2)
如果您有大量记录,则使用replace()计算排序键将无法扩展。
最好的方法是添加一个包含标题的附加表字段,其中删除了A / An /等前缀,并确保它有一个索引来加快排序。然后您可以按此新字段进行排序,但显示原始未更改的字段。
答案 1 :(得分:1)
也许这样的事情。
;with T(Title) as
(
select 'The Ancient Alligator' union all
select 'Aunt Annie''s Alligator' union all
select 'A Complete Guide to Alligators' union all
select 'Countrified Alligators' union all
select 'Don''t Touch the Alligator!' union all
select 'An Effortless Alligator Hunt'
)
select Title
from T
order by replace(
replace(
replace(T.Title,
'A ', ''),
'An ', ''),
'The ', '')
结果:
Title
------------------------------
The Ancient Alligator
Aunt Annie's Alligator
A Complete Guide to Alligators
Countrified Alligators
Don't Touch the Alligator!
An Effortless Alligator Hunt