First Business Day of the Current Month - SQL Server

First business day of the current month - SQL Server

A Simple case statement could do it

SELECT CASE 
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Saturday'
THEN dateadd(mm, DATEDIFF(MM, 0, getdate()), 0) + 2
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Sunday'
THEN dateadd(mm, DATEDIFF(MM, 0, getdate()), 0) + 1
ELSE dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)
END

Finding the first and last business day for every month sql

If you got already a table with all the weekdays only:

select min(datecol), max(datecol)
from BusinessOnlyCalendar
group by year(datecol), month(datecol)

But you should expand your calendar to include all those calculations you might do on date, like FirstDayOfWeek/Month/Quarter/Year, WeekNumber, etc.

When you got a column in your calendar indicating business day yes/no, it's a simple:

select min(datecol), max(datecol)
from calendar
where businessday = 'y'
group by year(datecol), month(datecol)

How can I select the first day of a month in SQL?

SELECT DATEADD(month, DATEDIFF(month, 0, @mydate), 0) AS StartOfMonth

Sql Server agent Job the first business day of each month

Under Frequency, you can choose "The first weekday of every 1 month(s)".
SQL Agent New Job Schedule

It won't handle holidays, but as far as I know that's the closest you can get with the schedule configuration.

Select the first weekday of the next Month

Your query implementation looks like for SQL server. You may use DATEFROMPARTS or EOMONTH function to get first day of next month, then use DATENAME in CASE expression to decide the first weekday like below (SQL Server only).

-- get first day of next month using DATEFROMPARTS
Declare @NextMonthFirstDate datetime = DATEFROMPARTS(YEAR(GETDATE()),MONTH(GETDATE())+1,1)

-- then use CASE expression
Select CASE WHEN DATENAME(WEEKDAY, @NextMonthFirstDate) = 'Sunday'
THEN @NextMonthFirstDate + 1
WHEN DATENAME(WEEKDAY, @NextMonthFirstDate) = 'Saturday'
THEN @NextMonthFirstDate + 2
ELSE @NextMonthFirstDate END as FirstWeekDayOfnextMonth


Related Topics



Leave a reply



Submit