Solved

Go Back 5 business Days

Posted on 2013-12-19
5
509 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

895 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

11 Experts available now in Live!

Get 1:1 Help Now