Solved

SQL query for previous month - cannot use dateadd

Posted on 2008-10-16
10
464 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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