Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current month firstdate and lastdate in postgres sql query

I want to get first and last dates of current month (Like 30th or 31st).How to get this one by using postgress sql query.

like image 912
suresh Avatar asked Apr 14 '17 07:04

suresh


People also ask

How do you get the last date of the month in Postgres?

Example# 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 I get the current date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format. In this format, 'YYYY' is a 4-digit year, 'MM' is a 2-digit month, and 'DD' is a 2-digit day. The returned value is a date data type.

How do I select data between two dates in PostgreSQL?

Use the PostgreSQL AGE() function to retrieve the interval between two timestamps or dates. This function takes two arguments: the first is the end date and the second is the start date.


1 Answers

First day is easy...

SELECT date_trunc('month', CURRENT_DATE);

Last day isn't much more difficult either.

SELECT date_trunc('month', CURRENT_DATE) + interval '1 month - 1 day';
like image 60
NO WAR WITH RUSSIA Avatar answered Oct 06 '22 01:10

NO WAR WITH RUSSIA