如何获取上月数据和月至今数据 [英] How to get the last month data and month to date data

查看:55
本文介绍了如何获取上月数据和月至今数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助编写查询以获取上个月的数据以及本月至今的数据.

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

如果今天的日期是 2011 年 3 月 23 日,我需要检索上个月的数据和今天的数据(即 2011 年 3 月 23 日).

If today's date is Mar 23 2011, I need to retrieve the data from last month and the data till todays date(means Mar 23 2011).

如果日期为2011年4月3日,则数据应包括3月月份数据和截至2011年4月3日的数据.

If date is Apr 3 2011, data should consists of March month data and the data till Apr 3rd 2011.

谢谢,

沙赫拉

推荐答案

Today including time info  : getdate()
Today without time info    : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 0)
Tomorrow without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Beginning of current month : DATEADD(month, datediff(month, 0, getdate()), 0)
Beginning of last month    : DATEADD(month, datediff(month, 0, getdate())-1, 0)

很有可能

WHERE dateColumn >= DATEADD(month, datediff(month, 0, getdate())-1, 0)
  AND dateColumn <  DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)

这篇关于如何获取上月数据和月至今数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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