Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Script to Return Last Weeks Data

By SALIM ALI,

I was asked by a customer to create a scheduled weekly report detailing work that had been completed in the previous week (Monday to Friday) I figured they might lose the report or something might happen to stop the scheduler from running it, and I didn't want to have to modify my script to work out date ranges for the selection criteria if I ran the report later in the current week.  So the script below works with a variable date range based on a calculation of the current weekday. The dates are converted to a varchar(12) to truncate timestamps.

However, this is only good if you run the report on a weekday (Mon-Fri), it kinda  falls over if you run it on a weekend as it will include weekend dates which is easily fixed by filtering the dates to ignore weekdays 1 & 7.  But then, who works weekends?

Total article views: 120 | Views in the last 30 days: 3
 
Related Articles
FORUM

get weekday in mdx

get weekday from dim value (string? not date)

ARTICLE

Finding the Correct Weekday Regardless of DateFirst

In this article, learn how to get the correct weekday regardless of the DateFirst setting on your se...

FORUM

TSQL copy data in weekdays

TSQL copy data in weekdays

SCRIPT

UDF_GET_NEXT_WEEKDAY

A simple way of getting the next weekday's dates...

Tags
miscellaneous    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones