获取上个月在 SQL Server 中的记录 [英] Get the records of last month in SQL server

查看:29
本文介绍了获取上个月在 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 中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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