Solved

Go Back 5 business Days

Posted on 2013-12-19
5
518 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:
Scott Pletcher 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

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

685 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