Solved

SQL query for previous month - cannot use dateadd

Posted on 2008-10-16
10
467 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
[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
  • 4
  • 3
10 Comments
 
LVL 143

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 143

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
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.

 

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 143

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 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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