To select the first day of a month in SQL, use a date function based on the database system. Here’s how to do it for common SQL databases:
1. MySQL:
SELECT DATE_FORMAT(NOW(), ‘%Y-%m-01’) AS first_day;
Or use:
SELECT LAST_DAY(NOW() – INTERVAL 1 MONTH) + INTERVAL 1 DAY AS first_day;
2. PostgreSQL:
SELECT DATE_TRUNC(‘month’, CURRENT_DATE) AS first_day;
3. SQL Server:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS first_day;
4. Oracle:
SELECT TRUNC(SYSDATE, ‘MONTH’) AS first_day FROM DUAL;
These functions return the first day of the current month. Adjust the date input as needed for specific months.