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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.