Solved

Go Back 5 business Days

Posted on 2013-12-19
5
522 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
[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
5 Comments
 
LVL 53

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

632 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