Solved

Go Back 5 business Days

Posted on 2013-12-19
5
504 Views
Last Modified: 2013-12-20
Seeing if anyone knows how to query back 5 business days?

business days meaning no weekends.

So if I ran today(12/19) it would return a date of 12/13/2013
0
Comment
Question by:patriotpacer
5 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39730076
In your where clause can you use

 WHERE DATEPART(dw,'<<some date>>') <> 1 AND DATEPART(dw,'<<some date>>') <> 7
0
 
LVL 8

Expert Comment

by:vr6r
ID: 39730104
DECLARE @daysback INT = 5

WHILE(DATEPART(DW, GETDATE() - @daysback) IN (1,7))
BEGIN
	SET @daysback = @daysback + 1
END

SELECT CONVERT(DATE,GETDATE() - @daysback, 101)

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 39730119
Easiest is to first create a "NonWorkDays" table.  Put in all nonwork days, including weekends and holidays.

Then you can do something like this:

DECLARE @start_date datetime
DECLARE @num_of_days_back int

SET @start_date = GETDATE()
SET @num_of_days_back = 5

;WITH
cteDigits AS (
    SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
),
cteTally AS (
    SELECT [10s].digit * 10 + [1s].digit AS tally
    FROM cteDigits [1s]
    CROSS JOIN cteDigits [10s]
)
SELECT TOP (1) date_to_check
FROM (
    SELECT TOP (@num_of_days_back) date_to_check
    FROM cteTally t
    CROSS APPLY (
        SELECT DATEADD(DAY, -t.tally, @start_date) AS date_to_check
    ) AS ca1
    WHERE
        t.tally BETWEEN 0 AND @num_of_days_back * 4 AND
        NOT EXISTS(SELECT 1 FROM dbo.NonWorkDays nwd WHERE nwd.NonWorkDay = date_to_check)
    ORDER BY date_to_check DESC
) AS possible_dates
ORDER BY date_to_check ASC
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39730156
declare	@dateStart	datetime
,	@dateEnd	datetime
,	@counter	int

-- chop off time, initialize variable
select	@dateStart	= convert(varchar,current_timestamp,101)
,	@dateEnd	= convert(varchar,current_timestamp,101)
,	@counter	= 1

-- go back 5 days
While @counter <= 5 begin
	select	@dateStart	= dateadd(dd,-1,@dateStart)
	,	@counter	= @counter + case when datepart(weekday,@dateStart) in (1,7) then 0 else 1 end
end

-- show results
select	@dateStart	
,	@dateEnd	

Open in new window

0
 

Author Closing Comment

by:patriotpacer
ID: 39732186
Thank you so much for all the contributions.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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…

762 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

20 Experts available now in Live!

Get 1:1 Help Now