Back to cookbooks list Articles Cookbook

How to Get the Previous Month in T-SQL

  • CURRENT_TIMESTAMP
  • DATEADD()
  • MONTH()

Problem:

You would like to display the previous month (without time) in a SQL Server database.

Solution:

SELECT MONTH(DATEADD(MONTH, -1, CURRENT_TIMESTAMP));

Discussion:

To get the previous month in SQL Server, subtract one month from today's date and then extract the month from the date. First, use CURRENT_TIMESTAMP to get today's date. Then, subtract 1 month from the current date using the DATEADD function: use MONTH as the date part with -1 as the parameter. Finally, extract the month from the received date using the MONTH() function.

Recommended courses:

Recommended articles:

See also: