Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First business day of the current month - SQL Server

Tags:

sql

sql-server

How could I get the first business day of the current month? Without create a function, only select. something like that:

SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1), GETDATE()), 101)

somebody knows please?

Thanks.

like image 494
Deiwys Avatar asked Nov 24 '14 16:11

Deiwys


People also ask

How do I get the first day of the current month in SQL Server?

To find the first day, the date time is first converted to date only using TO_DATE function. Then, the date is changed to the first day of the month by using the DATE_FROM_PARTS function. This works by inputting 01' in the 'day' part of the function.

Is there a beginning of month function in SQL?

There is no straightforward way or built-in function to get the first day of the month for a specific date or current date in SQL Server. To get it, we have to write our own script.


1 Answers

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
like image 78
suman Avatar answered Nov 14 '22 21:11

suman