Dynamic Date Ranges using CASE WHEN in SSMS

Stop hardcoding dates. Use CASE WHEN to automatically pull trailing windows.

SQL

SSMS Automation

Stop Hardcoding Dates!

Using a CASE WHEN statement inside your WHERE clause can save you from manually updating your queries every reporting cycle.

SELECT * 
FROM sales 
WHERE sale_date >= CASE 
    WHEN DAY(GETDATE()) = 1 THEN DATEADD(month, -1, GETDATE())
    ELSE GETDATE() 
END;

This ensures your trailing 30-day logic never breaks on the first of the month.