Connect With MyTechMantra.com









"Receive newsletters and special offers about SQL Server, BizTalk and SharePoint from MyTechMantra. We respect your privacy and you can unsubscribe at any time."




SQL Server Query to Find First Day and Last Day of Month


August 22, 2012


Introduction

There can be a scenario where you need to load data in batch into warehouse for the current month. During such scenarios identifying the first and last day of the month becomes necessary. This article includes the TSQL script specific to find first and last date of a month in SQL Server 2008 and higher versions.

Find First Day & Last Day of the Month in SQL Server 2008/2008R2

Execute the below TSQL script to find first day & last day of the month in SQL Server 2008/2008R2.

DECLARE @CurrentDate AS DATE = GETDATE()

/* First Day of the Month */
SELECT DATEADD(DD, -DAY(@CurrentDate) + 1, @CurrentDate) AS FirstDayOfTheMonth

/* Last Day of the Month */
SELECT DATEADD(DD, -DAY(DATEADD(MM, 1, @CurrentDate)), DATEADD(MM, 1, @CurrentDate)) AS LastDayOfTheMonth


SQL Server Query to Find First Day and Last Day of Month

For more information on New Data and Time Functions in SQL Server 2008/2008 R2 see here.

Retrieve First Day & Last Day of the Month in SQL Server 2012

Execute the below TSQL script to retrieve first day & last day of the month in SQL Server 2012.

DECLARE @CurrentDate AS DATE = GETDATE()

/* First Day of the Month */
SELECT DATEADD(DD, -DAY(@CurrentDate) + 1, @CurrentDate) AS FirstDayOfTheMonth

/* Last Day of the Month */
SELECT EOMONTH(@CurrentDate) AS LastDayOfTheMonth


SQL Server Query to Find First Day and Last Day of Month

For more information on EOMONTH and other New Data and Time Functions in SQL Server 2012 see here.

Share this Article

LEARN MORE...
 
Winners
White Papers
Product Reviews
Trending News
All Articles
Free Tools
 
Follow Us...

Related Articles…