0 Replies - 1726 Views - Last Post: 26 December 2008 - 10:25 PM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

MSSQL user defined function to find first weekday of any given month

Posted 26 December 2008 - 10:25 PM

Description: Pass the month you want the first weekday forThis is a snippet to find the first weekday of any given month
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION dbo.udfFindFirstWeekdayOfMonth (@MonthToCheck DATETIME)
RETURNS VARCHAR(10)	
	BEGIN
		/*
		This is the variable that will hold the name of the
		first weekday be returned from our function
		*/
		DECLARE @WeekDay VARCHAR(10)
		SELECT 
			@WeekDay = DATENAME(dw, DATEADD(dd, - DATEPART(dd, @MonthToCheck) + 1, @MonthToCheck))
			IF(@WeekDay IN ('Saturday', 'Sunday'))
			BEGIN
				SET @WeekDay = 'Monday'
			END
		RETURN @WeekDay
	END


Is This A Good Question/Topic? 0
  • +

Page 1 of 1