sqlsql-server

How to get 22nd date of the previous month in SQL


I want to add a column on my query which will provide me the 22nd day of the previous month. I can query first day and last day of the month but not sure how to bring the 22nd day of the previous month.

For example: 22/09/2024

How can I achieve this?

I want the query to return 22nd day of the previous month.


Solution

  • We can use DATEADD() and DATEDIFF() functions as follows:

    SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 21)  -- 2024-09-22