DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to find the first work day of a month?

Execute the following Microsoft SQL Server Transact-SQL (T-SQL) script in Management Studio (SSMS) Query Editor, SQLCMD or other client software to create a user-defined function (UDF) to calculate the first work day in a month:


use AdventureWorks2008;

go

create function fnFirstWorkDayOfMonth( @Year int, @Month int)

returns datetime

as

begin

declare @FirstDayOfMonth datetime

set @FirstDayOfMonth = convert(datetime, convert(varchar,@Year)+'-'+convert(varchar,@month)+'-01')

if datepart(weekday,@FirstDayOfMonth) not in (1,7)

      return (@FirstDayOfMonth)

set @FirstDayOfMonth = dateadd(day,1,@FirstDayOfMonth)

if datepart(weekday,@FirstDayOfMonth) not in (1,7)

      return (@FirstDayOfMonth)

      return (dateadd(day,1,@FirstDayOfMonth))

end

go

 

select dbo.fnFirstWorkDayofMonth (

      year(getdate()),

      month(getdate())

      )

go

 

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE