Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamic Query to pull records for the previous month

Posted on 2003-02-27
5
Medium Priority
?
218 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
4 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

564 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