4

在 SQLSERVER/MSSQL 中,问题如下:

SELECT * from [Translation Color] order by [Language Code] 

我想要以“I”字母开头的字母顺序排列的记录。

结果示例:

'Ioren' 'Iumen' 'Tart' 'Arfen' 'Coldry'

我不想使用联合或更多的 sql 语句.. 只是尝试用特殊子句的顺序来捕获它。

我试过:

ORDER BY <field> REGEXP '^I' DESC

但它没有用。

有任何想法吗?

4

3 回答 3

3

这应该这样做:

ORDER BY CASE WHEN SUBSTRING([Translation Color],1,1) = 'l' 
     THEN 1 ELSE 0 END DESC

编辑:

完全从 i 开始排序,然后循环回到 h 的完整答案是:

ORDER BY CASE WHEN ASCII(UPPER(SUBSTRING([Translation Color],1,1))) < 73 
         THEN ASCII(UPPER(SUBSTRING([Translation Color],1,1))) + 26
         ELSE ASCII(UPPER(SUBSTRING([Translation Color],1,1))) END ASC,       
         [Translation Color] ASC

请注意,这将影响大型表的性能。

于 2009-04-02T09:27:31.320 回答
2

或者这有什么好处:

select [Translation Color], 
  case when [Translation Color] < 'l' then 1
                     else 0 
                     end as Priority
from t1 
order by Priority, [Translation Color]

这将从'l'开始按字母顺序排列

编辑 这个解决方案似乎对我有用:

create table t1 ( c1 varchar(20) collate SQL_Latin1_General_Cp437_CI_AS)

然后我填充了一些测试数据然后运行:

select c1 
from t1 
order by case when c1 >= 'l' then 0 else 1 end, c1
于 2009-04-02T10:01:02.267 回答
2
SELECT *
FROM [Translation Color]
ORDER BY
    CASE WHEN [Language Code] LIKE '[I-Zi-z]%' THEN 0 ELSE 1 END,
    [Language Code]
于 2009-04-02T10:26:38.243 回答