Calculate Total Number Of days for given month in SQL Server


Step 1:- Create SQL Function



/****** Object:  UserDefinedFunction [dbo].[getTotalDaysInMonth]    ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Author:
-- Create date: <25th July, 2009>
-- Description:
-- =============================================
CREATE FUNCTION [dbo].[getTotalDaysInMonth]
(
	-- Add the parameters for the function here
	@anydateofMonth DATETIME
)
RETURNS INT
AS
BEGIN
	-- Declare the return variable here
	DECLARE @totalDaysInMonth INT
	-- Add the T-SQL statements to compute the return value here
 
		DECLARE @givendate DATETIME
		SET @givendate = @anydateofMonth
 
		SET @givendate = STR(YEAR(@givendate)) + '-' + STR(MONTH(@givendate) + 1) + '-01' 
 
		SELECT @totalDaysInMonth = DATEPART(dd, DATEADD(DAY, -1, @givendate))
 
	-- Return the result of the function
	RETURN @totalDaysInMonth
 
END




Step 2:- Write SQL query

SELECT dbo.getLastBusinessDay(GETDATE()) AS TotalDaysInMonth



Comments

Popular posts from this blog

GROUP BY, CUBE, ROLLUP and SQL SERVER 2005

How to get content of Ckeditor & Fckeditor using Javascript

How to Fix Error- Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out