Get the last day of the month in PostgreSQL

Find the last day of the Month in PostgreSQL

Check the last day of the current month in PostgreSQL

postgres=# SELECT (DATE_TRUNC('MONTH', ('20230401')::DATE) + INTERVAL '1 MONTH - 1 day')::DATE;
    date
------------
 2023-04-30
(1 row)

Check the last date of the current date in PostgreSQL

postgres=# SELECT (DATE_TRUNC('MONTH', (now())::DATE) + INTERVAL '1 MONTH - 1 day')::DATE;
    date
------------
 2023-04-30
(1 row)

Check the last day of next month

postgres=# SELECT (DATE_TRUNC('MONTH', (now())::DATE) + INTERVAL '2 MONTH - 1 day')::DATE;
    date
------------
 2023-05-31
(1 row)
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.