Days in Month

Posted: November 21, 2011 in SQL Server 2005

To determine the number of days in a month, you can try the following:

-- Declare variables
DECLARE @ldt_date DATETIME;
DECLARE @li_days  INT;

-- Initialise variables
SET @ldt_date = GETDATE();

-- Calculate days in month
SET @ldt_date = (@ldt_date - (DAY(@ldt_date) - 1));
SET @ldt_date = DATEADD(mm, 1, @ldt_date);
SET @ldt_date = DATEADD(dd, -1, @ldt_date);
SET @li_days = DATEPART(dd, @ldt_date);

-- Return days in month
SELECT @li_days;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s