How to get first & last day of the previous month


There are many scenarios where we need to get the first and last day of the previous month. Suppose we want to see the order summary of the previous month, and if we write a simple query, it means we will get the result of the last thirty days, but I want to get the order summary of last month. Suppose today is 12/15/2014, and if I want to see the order summary of last months, I mean from date 11/01/2014 to 11/30/2014. We could apply the date (11/01/2014 to 11/30/2014) directly, but one issue may arise here the total days of the months, It may vary from 28 to 31 days, that are the reason we need to get the first and last day of the previous month.


The below-given script may help you.

Get the first day of the previous month


This query will return the first day of the previous month
SELECT CONVERT(DATE,DATEADD(MM, DATEDIFF(MM, 0, GETDATE())-1, 0)) AS FirstDayOfPreviousMonth

Get the last day of the previous month


This query will return the last day of the previous month

SELECT CONVERT(DATE,DATEADD(MS, -3, DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) , 0))) AS LastDayOfPreviousMonth


Get the first day of the current month


To get the first day of the current month use the following query
SELECT CONVERT(DATE,DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0)) AS FirstDay

Get the last day of the current month


To get the first day of the current month use the following query

SELECT CONVERT(DATE,DATEADD(MS, -3, DATEADD(MM, DATEDIFF(MM, 0, GETDATE())+1 , 0))) AS LastDay

NUnit : Introduction

There are several unit test tools in the market, but one of them is NUnit, the most popular tool for doing unit testing. So in this tutorial...