Solved

SQL query for previous month - cannot use dateadd

Posted on 2008-10-16
10
460 Views
Last Modified: 2012-08-13
We are running reports in an SAP add-on called XL Reporter. SAP is based on a SQL 2005 database.

We need to have a SQL query that will select data from a field based on the previous month. In the past we have accomplished this with the following query: WHERE [U_Date] >= '@Start_Date' AND [U_Date]<='@End_Date'. In order to schedule automated reporting, we need this query to be dynamic and based on the previous month from the date the report is run.

We have attempted to run using dateadd, but the XL Reporter software apparently does not recognize this command. We have been able to run a successful query using WHERE MONTH(U_Date) <= GETDATE(). Any ideas how to accomplish our objective using the GETDATE() function?
0
Comment
Question by:shep_cj
  • 4
  • 3
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22736003
what about this:
WHERE U_Date >= DATEADD(MONTH, DATEADD(DAY, 1-DATEPART(DAY, GETDATE()), CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120)))
  AND U_Date < DATEADD(DAY, 1-DATEPART(DAY, GETDATE()), CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120))

Open in new window

0
 

Author Comment

by:shep_cj
ID: 22736096
Hello Angellll:

Thanks for the quick response. Two items to consider:

1. The dateadd function does not appear to work in XLR.
2. We only need the months to match.

Any ideas on using this without dateadd?

Thanks!
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22736134
I see. that's the reason you put that in the question title.

now, I would suggest you put the entire SQL into a sql server VIEW, so the reporting tool just get's the data it needs,and does not have to matter about what syntax is used below. i would basically just use the SELECT * FROM viewname, in the end..

now: We only need the months to match.
does that mean, you want to get all the data from any year for that month?
/this will not change the technique to use a view, or a function,btw ...
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:shep_cj
ID: 22736349
We need to get all data for the previous month, from the same year, for a given sales representative.

Thanks!

Chris
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22736369
>for a given sales representative.
ok, that sounds a function (for the parameter?) ...

anyhow, is that the way you want to go?
0
 

Author Comment

by:shep_cj
ID: 22736474
Correct. Here is an example of a query that works and is based on the month of August (8). Keep in mind that this add-on does not require the use of 'WHERE'.

[U_Rep]='@Rep_Parameter' AND MONTH(U_Date)=9.

Any ideas how to replace 9 with a  dynamic function?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22737946
>Any ideas how to replace 9 with a  dynamic function?
that would be datepart, actually.
however, MONTH(U_DATE) alone has 2 problems:
* I will not consider the YEAR (which is your requirement)
* I will not be able to use an index on u_date field, if any.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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