愿所有的美好和期待都能如约而至

SQL server 获取上个月的记录

发布时间:  来源:互联网  作者:匿名  标签:error Get the records of last month in SQL server exception IT资讯 sql sql-server   热度:37.5℃

本文介绍了SQL server 获取上个月的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的数据库表 [member] 字段date_created”获取上个月的记录.

I want to get the records of last month based on my db table [member] field “date_created”.

执行此操作的 sql 是什么?

What’s the sql to do this?

为了澄清,上个月 – 1/8/2009 至 31/8/2009

For clarification,
last month – 1/8/2009 to 31/8/2009

如果今天是 3/1/2010,我需要获取 1/12/2009 到 31/12/2009 的记录.

If today is 3/1/2010, I’ll need to get the records of 1/12/2009 to 31/12/2009.

推荐答案

SELECT * 
FROM Member
WHERE DATEPART(m, date_created) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(yyyy, date_created) = DATEPART(yyyy, DATEADD(m, -1, getdate()))

您需要检查月份和年份.

You need to check the month and year.

这篇关于SQL server 获取上个月的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

勇敢去编程!

勇敢的热爱编程,未来的你一定会大放异彩,未来的生活一定会因编程更好!

TOP