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 获取上个月的记录
基础教程推荐
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
