How to Get the First Day and Last Day of the Month in SQL
Learn how to easily retrieve the first and last day of the month in SQL using DATE_TRUNC and interval arithmetic.
350 views
To get the first day of the month in SQL: `SELECT DATE_TRUNC('month', current_date);`. For the last day of the month: `SELECT DATE_TRUNC('month', current_date) + INTERVAL '1 month' - INTERVAL '1 day';`.
FAQs & Answers
- How do I find the first day of the current month in SQL? You can use the DATE_TRUNC function as follows: SELECT DATE_TRUNC('month', current_date); which returns the first day of the current month.
- What is the easiest way to get the last day of the month in SQL? Use DATE_TRUNC with interval arithmetic: SELECT DATE_TRUNC('month', current_date) + INTERVAL '1 month' - INTERVAL '1 day'; to get the last day of the current month.
- Can I use these SQL methods in all database systems? DATE_TRUNC and interval syntax are commonly supported in PostgreSQL and some other systems, but syntax may vary. Always check your specific SQL database documentation.