Solved

get weekly report

Posted on 2012-03-30
3
149 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
3 Comments
 
LVL 32

Expert Comment

by:bhess1
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

747 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

9 Experts available now in Live!

Get 1:1 Help Now