Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL query for previous month - cannot use dateadd

Posted on 2008-10-16
10
Medium Priority
?
478 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 2000 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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