简体   繁体   English

按年份排序为 nvarchar,然后按数字排序

[英]Sort by year as nvarchar and then numerically

I have an nvarchar column that in essence stores the following data:我有一个nvarchar列,它本质上存储以下数据:

20-198
99-135
19-135
20-197
20-195
99-435

The first two numbers represent the year created and the last numbers represent the id of the unit made that year.前两个数字代表创建的年份,最后一个数字代表当年制造的单位的 ID。 Whenever I sort I get the 99's first and 19 last.每当我排序时,我都会得到 99 的第一个和 19 的最后一个。 I'd like to sort by latest year first and then numerically.我想先按最近一年排序,然后再按数字排序。

I have tried converting to datetime and using sub-strings but I cannot create a valid query and I'm not really sure where to go from here.我已尝试转换为datetime时间并使用子字符串,但我无法创建有效的查询,而且我不确定从这里到 go 的位置。

Any help would be appreciated!任何帮助,将不胜感激!

You can use conditional logic in the order by :您可以按以下order by使用条件逻辑:

order by (case when col <= '21' then 1 else 2 end),
         col desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM