如何获得当年的第一个月 [英] How to get first month of current year

查看:120
本文介绍了如何获得当年的第一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想从当前日期起一年内按月返回数据.
也就是说,如果当前日期为2012-08-27,则数据应从2012-01-01返回到2012-08-27.
所以在这里我无法获得当年的第一个月,即"2012年1月"
请提供所需的查询.以下查询是为了获取每月的数据,找出我做错了什么地方.

Hi,
I want to return data in monthwise in a year from current date.
i.e if current date is 2012-08-27 the data should be return from 2012-01-01 to 2012-08-27.
so here i am not able to get the first month of the current year i.e "jan 2012"
please give required query. The following query is to get monthwise data plese find out where i have done wrong.

SELECT
           DATEPART(MONTH, convert(DATETIME,CreatedDateTime,13)) AS [Month of Year]
         , Id AS [TotalShipOrders]
      FROM ShipmentDetail
      WHERE CONVERT(VARCHAR(7),CreatedDateTime,111)= convert(varchar(7),@CurrentDate,111)
      UNION ALL
      SELECT Month, NULL
      FROM @Months




问候,
Raghu.




Regards,
Raghu.

推荐答案

hi,

一年中的第一个月总是一样.
因此,您只需要查找当前年份并连接字符串即可.然后将该字符串转换为日期时间.所以会找到想要的日期.

谢谢,
Viprat


first month of the any year will always same.
so you had to just find current year and concate string. then convert that string to datetime. so will find desired date.

Thanks,
Viprat


选择DATEADD(年,-1 * DateDiff(年,getdate(),0),0)
select DATEADD(Year,-1*DateDiff(Year,getdate(),0),0)


看到它. /> http://social.msdn.microsoft.com/Forums/zh/transactsql/thread/48aaab1f-633a-464b-a7fb-b8f7abfe9002 [
see it..
http://social.msdn.microsoft.com/Forums/en/transactsql/thread/48aaab1f-633a-464b-a7fb-b8f7abfe9002[^]


这篇关于如何获得当年的第一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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