t-sql first day of previous month

If you are after T-SQL First Day of Previous Month calculation then you are in the right place.

To return first day of previous month using T-SQL we can use the following statement:

Below is a visual representation of the logic I want to use to get first day of previous month and below the picture is the explanation.

First Day of Previous month

1) Our today's date is 20th of Feb. We substract number of days in the month from our date (20) and we get last day of previous month.
2) We add one day to get first day of current month.
3) We go one month back so first day of the month stays the same and Feb changes to Jan.

Now the code:

SELECT DATEADD(m,-1, Dateadd(d,1-DATEPART(d,getdate()),GETDATE())) AS [First Day of Previous Month]

The result of this T-SQL statement is first day of previous month.

Hope that helps!
Katie

Did you find this page useful? +3  |  -0
(3 Votes)

it works accurately
Want to add a comment


no comments
Donations: $145 (30 days)
Next Target: $250
Achieved: 72%

30 days stats
Thank you to your donation!

21st Aug - John Chamblee ($50)
20th Aug - Eamon Heenan (£10)
20th Aug - Pawel Olejniczak ($20)
19th Aug - Robert French ($10)
19th Aug - Robert Kelly ($10)
16th Aug - Lee Mezzulo ($5)
8th Aug - Betrehail Keteam ($2)
7th Aug - Pawel Bednarski (£10)
27th Jul - Bernhard Lauber (€20)
----
15th Jul - Pawel Olejniczak (£20)
13th July - Thomas Tabert ($10)
30 days stats
#1: Server $82/month
#2: Video hosting $25/month
#3: Katie & Emil $0.00/h
Caring is sharing :)