在sql server中获取替代月份和年份 [英] Get alternative month and year in sql server

查看:52
本文介绍了在sql server中获取替代月份和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在sql中查询,我的查询如



 选择 [月],[年] 来自 [dbo]。[ScoreCard_Accounts] 其中(年份= '  2015' 年= span class =code-string>'  2014'服务   null   order   by 年(年) asc ,DATEPART(mm,CAST([Month] + '  1900'  AS   DATETIME ))  asc  





但是我的结果低于结果

 2015年1月
2014年1月
2014年2月
2015年2月
2015年3月
2014年3月
2014年4月
2015年4月





但我需要像



 2014年1月
2015年1月
2014年2月
2015年2月
2014年3月
2015年3月
2014年4月
2015年4月........





请帮忙如何在sql中编写查询



谢谢

Pradeep

解决方案

在任何 WHERE ORDER BY 子句>子句

您必须计算一个 MONTH int列,这样您就可以按第一年和第二年的顺序排序。



你可以聪明或有一个

  ORDER   BY  
CASE monthColumn
WHEN ' Jan' 然后 1
...
WHEN ' Dec' 12
end ),
yearColumn


Hi i am wring an query in sql,my query like

select [Month],[Year] from [dbo].[ScoreCard_Accounts] where (Year='2015' or Year='2014') and Service is null order by Year(Year) asc,DATEPART(mm,CAST([Month]+ ' 1900' AS DATETIME)) asc



But i am getting below result

Jan	2015
Jan	2014
Feb	2014
Feb	2015
Mar	2015
Mar	2014
Apr	2014
Apr	2015



But i need like

Jan 2014
Jan 2015
Feb 2014
Feb 2015
Mar 2014
Mar 2015
Apr 2014
Apr 2015........



Please any one help how to write query in sql

Thanks
Pradeep

解决方案

use an ORDER BY clause after any WHERE clause
You will have to calculate a MONTH int column so you can order by that first and the year second.

You can be clever or have an

ORDER BY
  (CASE monthColumn
      WHEN 'Jan' then 1
      ...
      WHEN 'Dec' then 12
   end ),
   yearColumn


这篇关于在sql server中获取替代月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆