我有一个年份列,其中包含字符串值,如2015,2014,2013,2012等。显示字符串值的月份列,如1月,2月,... 12月。考虑到Year和month列,我需要运行一个对表进行排序的选择。谁能提供一些输入?是否有任何系统程序可以轻松完成。
我希望输出基于年份和月份降序。
答案 0 :(得分:2)
您可以执行以下操作:
order by cast(year as int) desc
,case when month = 'January' then 1
when month = 'February' then 2
....
when month = 'December' then 12
end desc
答案 1 :(得分:1)
试试这个
ORDER BY CAST(month+year AS DATE) DESC
答案 2 :(得分:0)
试试这个
DECLARE @table TABLE
(
year VARCHAR(10),
month VARCHAR(20)
)
INSERT INTO @table
VALUES ('2015',
'January'),
('2015',
'March'),
('2015',
'February'),
('2015',
'April'),
('2015',
'May'),
('2014',
'January'),
('2014',
'February'),
('2014',
'March'),
('2014',
'May'),
('2014',
'April'),
('2016',
'January'),
('2016',
'February'),
('2016',
'March'),
('2016',
'May'),
('2016',
'April')
SELECT *
FROM @table
ORDER BY CONVERT(INT, year) desc,
Datepart(mm, Cast(month + ' 1900' AS DATETIME)) desc
答案 3 :(得分:0)
您需要将月份名称翻译为数字才能使用翻译参考表并存储值
jan-1,feb-2,march-3 like wise
然后尝试
SELECT * FROM tblName ORDER BY yearField DESC, monthField DESC