SQL server 获取上个月的记录

2021-12-10 00:00:00 sql tsql 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.

相关文章