Solved

get weekly report

Posted on 2012-03-30
3
156 Views
Last Modified: 2012-04-15
I want to create a process (weekly at 6pm)  which retreive data from last Friday at 6pm to Friday 6 pm.

select *from mytable where  myorderdate = getdate()-7 ?
0
Comment
Question by:VBdotnet2005
[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
3 Comments
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 37789614
Too hazardous.  You should not depend on something starting at exactly a specified instant.  Instead, use a range derived from the current date, e.g.:

SELECT *
FROM MyTable
WHERE MyOrderDate > CONVERT(varchar(8), DATEADD(DAY, -7, CURRENT_TIMESTAMP), 112) + ' 18:00' 
	AND MyOrderDate <= CONVERT(varchar(8), CURRENT_TIMESTAMP, 112) + ' 18:00'

Open in new window


Note that this is not the equivalent of a BETWEEN statement.  Using a BETWEEN clause, if an order fell at exactly 18:00 on Friday, then with a BETWEEN statement, it would be included in two weeks.  With this statement, it will only be included in one week.
0
 
LVL 6

Expert Comment

by:Ashok Kumar
ID: 37789978
- Try this.

WHERE (MyOrderDate >= DATEADD(day, -7, CURRENT_TIMESTAMP) and MyOrderDate <= now () )
0
 
LVL 9

Accepted Solution

by:
OCDan earned 500 total points
ID: 37790814
If run now it would select all orders between 24/03/2012 00:00:00 and 1/04/2012 00:00:00
Using the dateadd and datediff functions allows the query to still be SARG-able helping performance

SELECT *
FROM    mytable
WHERE  myorderdate BETWEEN DateAdd(day, datediff(day,0,getdate()-7), 0) and DateAdd(day, datediff(day,0,getdate()+1), 0)

If you wanted this to be sent out weekly automatically you could enable sp_send_dbmail

And setup a scheduled job in SQL Server Agent using the below code:
EXEC msdb.dbo.sp_send_dbmail
 @profile_name = 'ProfileName',
 @recipients = 'whomever@wherever.com',
 @query = 'SELECT * FROM    mytable  WHERE  myorderdate BETWEEN DateAdd(day, datediff(day,0,getdate()-7), 0) and DateAdd(day, datediff(day,0,getdate()+1), 0)' ,
 @subject = 'Query Output',
@attach_query_result_as_file = 1

Hope that is some help
0

Featured Post

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

632 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