?
Solved

Dynamic Query to pull records for the previous month

Posted on 2003-02-27
5
Medium Priority
?
217 Views
Last Modified: 2012-08-13
Hi

SQLServer2000 connection.Can anybody assist with the following:

A query that will automatically extract data for the previous month. I used this previously in MSAccess "Between DateSerial(Year(Date()),Month(Date())-1,1) And DateSerial(Year(Date()),Month(Date()),1)".This prevented me from using date prompts i.e I could run a macro in MSAccess with the above customised as a criteria.I am currently using a Business Intelligence tool

Thanks  
0
Comment
Question by:csshahie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Accepted Solution

by:
adathelad earned 200 total points
ID: 8032144
If you want records for the previous month try this as your WHERE clause:

WHERE DATEDIFF(mm, DateField, GETDATE()) = 1

Cheers
0
 
LVL 3

Expert Comment

by:hakyemez
ID: 8032522
DECLARE @dt SMALLDATETIME, @dt2 SMALLDATETIME

SET @dt = GETDATE() --NOW
SET @dt2 = DATEADD(MONTH,-1,@dt)

WHERE <field_name> BETWEEN @dt2 AND @dt

0
 

Expert Comment

by:CleanupPing
ID: 9276286
csshahie:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 34

Expert Comment

by:arbert
ID: 10910708
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.  

I will leave the following recommendation for this question in the Cleanup topic area:

Accept Answer From adathelad

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Arbert
EE Cleanup Volunteer
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question