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)".
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
How to Best Organize the Inner Joins in (Select) Statement
How to Query All Rows Within a 5-Mile Radius of My Coordinates
What's the Asymptotic Complexity of Groupby Operation
F# Type Provider for SQL in a Class
Self-Referencing Constraint in Ms SQL
SQL Server: Self-Reference Fk, Trigger Instead of on Delete Cascade
Dynamic Table Name in Select Statement
How to Merge Two or More Rows Based on Their Foreign Key
Syntax Error with Update Query When Join with Some Table
Pivot - SQL - Values from Subquery
How to Convert SQL to Relational Algebra in Case of SQL Joins
SQL Server Query Xml Attribute for an Element Value
Find Most Common Elements in Array with a Group By
Hibernate Create Criteria to Join the Same Table Twice - Tried 2 Approach with 2 Difference Error
Order by in a SQL Server 2008 View
Query for How to Add the Missing Dates in SQL
Ms Access: Setting Table Column Caption or Description in Ddl