Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LAST_DAY function in postgres

Tags:

postgresql

Is there any function(s) in postgres equivalent to Oracle function LAST_DAY().

I need to get last day in postgres (including month and year)

like image 221
jobi88 Avatar asked Jan 09 '13 05:01

jobi88


People also ask

How do I get the last day in PostgreSQL?

Use current_date to get today's date. Note that you don't need brackets at the end of the current_date function. In PostgreSQL, you can subtract or add any number of days using the INTEGER keyword. Here, since you need to subtract one day, you use - INTEGER '1' to get yesterday's date.

How do I get the last day of the month Postgres?

You can select the last day of month. SELECT (date_trunc('MONTH', ('201608'||'01')::date) + INTERVAL '1 MONTH - 1 day')::DATE; 201608 is replaceable with a variable.

How do dates work in PostgreSQL?

PostgreSQL uses the yyyy-mm-dd format for storing and inserting date values. If you create a table that has a DATE column and you want to use the current date as the default value for the column, you can use the CURRENT_DATE after the DEFAULT keyword.

How do I get the current date and time in PostgreSQL?

Postgresql now() The NOW() function in Postgresql is used to get the current date and time. The return type of the NOW() function is the timestamp with the time zone. We can fetch the current date and time by using the PostgreSQL NOW() function. This function has a return type i.e. the timestamp with the time zone.


1 Answers

Well, In postgres, it seems there's no such function equivalent to LAST_DAY() available in oracle.

If you need to, you can have your own in the following ways as a

Select Query

SELECT (date_trunc('MONTH', now()) + INTERVAL '1 MONTH - 1 day')::date;

plsql Function

        CREATE OR REPLACE FUNCTION last_day(date)
        RETURNS date AS
        $$
        SELECT (date_trunc('MONTH', $1) + INTERVAL '1 MONTH - 1 day')::date;
        $$ LANGUAGE 'sql'
        IMMUTABLE STRICT;

Hope this helps.

like image 67
Gopinagh.R Avatar answered Nov 05 '22 02:11

Gopinagh.R