Reputation: 233
The oracle last_day
function return the last day of the month.
example:
nls_date_format='YYYY-MM-DD H24:MI:SS'
select last_day(sysdate) from dual;
LAST_DAY(SYSDATE)
-------------------
2014-06-30 15:45:43
oracle return the time value as well.
I have tried below sql in PostgreSQL which return the last day of month but time value is "00:00:00".
select (date_trunc('month', now()) + interval '1 month -1 day')::timestamp(0);
?column?
---------------------------
2014-06-30 00:00:00
(1 row)
the sql return the date correctly but I want date and time like oracle.
Upvotes: 1
Views: 4564
Reputation: 2228
The function last_day() should do the trick. This function should be found in a extenstion package called "orafce". Containing other functions used in Oracle.
Install the correct version of orafce package (if needed):
apt-get install postgresql-9.1-orafce
SQL:
Create Extension orafce
If error rename orafce--3.0.sql to orafce--3.03.sql or user later version of orafce.
Use the function:
SELECT last_day('2015-01-01')
Works like expected and the result is '2015-01-31'
Upvotes: 0
Reputation: 125244
select (
date_trunc('month', now())
+ interval '1 month -1 day'
+ now()::time
)::timestamp(0);
Upvotes: 4