Solved

SQL query for previous month - cannot use dateadd

Posted on 2008-10-16
10
455 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:shep_cj
Comment Utility
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]
Comment Utility
>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
Comment Utility
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]
Comment Utility
>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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

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 …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now