0

What I need is the SQL code to display the last business day - usually will be one day prior, but I want that if I run this code on Monday that, instead of Sunday's date, I get Friday's date.

This is what I started with

- Automatically generate last business day
declare @LastBusinessDay varchar(8)
set @LastBusinessDay  = CONVERT(varchar(8), DATEADD(WEEKDAY,-1,GETDATE()), 112)
select @LastBusinessDay

I guess I thought that the WEEKDAY parameter in the DATEADD would only show dates that are from Monday - Friday. How can I achieve this

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by bajanpoet
0

Ok, after some more digging I found this

prev_bizday= dateadd(day, case when datepart(weekday, dte) = 1 then -2 
        when datepart(weekday, dte) = 2 then -3 else -1 end, dte)

where dte is the date you are looking for. (in my case, I put GETDATE() there

What I need is the SQL code to display the last business day - usually will be one day prior, but I want that if I run this code on Monday that, instead of Sunday's date, I get Friday's date.

This is what I started with

- Automatically generate last business day
declare @LastBusinessDay varchar(8)
set @LastBusinessDay  = CONVERT(varchar(8), DATEADD(WEEKDAY,-1,GETDATE()), 112)
select @LastBusinessDay

I guess I thought that the WEEKDAY parameter in the DATEADD would only show dates that are from Monday - Friday. How can I achieve this

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.