How to get the last month data and month to date data

后端 未结 4 1257
小鲜肉
小鲜肉 2020-12-28 21:50

Need help in writing the query to get the last month data as well as month to date data.

If today\'s date is Mar 23 2011, I need to retrieve the data from last month

4条回答
  •  天涯浪人
    2020-12-28 22:24

    Very helpful page

    declare @d datetime = '2011-04-03';
    
    declare @startDate datetime;
    select @startDate =
       CAST('01 '+ RIGHT(CONVERT(CHAR(11),DATEADD(MONTH,-1,@d),113),8) AS datetime);
    select @startDate;
    

提交回复
热议问题