Solved

get weekly report

Posted on 2012-03-30
3
154 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: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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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