Get the records of last month in SQL server(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 获取上个月的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL server 获取上个月的记录
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- SQL 临时表问题 2022-01-01