Wednesday, January 15, 2025
HomeProgrammingHow can I select the first day of a month in SQL?

How can I select the first day of a month in SQL?

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:

See also  Understanding MySQL Comments

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;

See also  How to Run PowerShell in CMD

These functions return the first day of the current month. Adjust the date input as needed for specific months.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x