EOMONTH equivalent in Postgresql - sql

Is there a EOMONTH() equivalent in Postgresql? If not, how do I select only the last day of the month for any given month and any given year given a time stamp? For example, I have a dataset with timestamps. From those times, I want to select only 2015-01-31, 2015-02-28, ... 2016-02-29, 2016-03-31, etc. What is an efficient way to do that?

One method is:
select date_trunc('month', col) + interval '1 month' - interval '1 day'

Related

PostgreSQL query to return the set of days between two dates

I need to return the dates between the 05 of the last month and the 05 of the current month Example today is the 16/08/2022 I recuperate therefore the whole of the days between the 05/07/2022 and the 05/08/2022
For the moment I try with this query
SELECT DATE from Db_name where date between date_trunc('month', current_date-1) and date_trunc('month',current_date)
Your initial query is very much on track. Just a couple additional things about dates:
current_date-1 subtracts 1 day, you need to subtract 1 month. Thus current_date - interval '1 month'.
date_trunc(somedate) returns the 1st of the month so
date_trunc('2022-08-17') returns 2022-08-01.
getting to the 5th of the month from the 1st just add interval '4 days'.
adding or subtracting intervals to dates results in timestamps.
Since you want dates so needs to be cast back to date.
select *
from db_name
where some_date between (date_trunc('month', current_date-interval '1 month') + interval '4 days')::date
and (date_trunc('month', current_date) + interval '4 days')::date;
But I will repeat do not any reserved word or data type as a name. At best is causes confusion, at worst it will run but do the wrong thing.

SQL Presto - Week function to star on a Sunday

I'm trying to extract the week number from a date, and I want the week to be counted from Sunday to Saturday. This is what I currently have, but I can't seem to find any solution for this is SQL Presto.
SELECT WEEK(date) AS weeknum
Can this be solved?
Thank you!
One method is:
select week(date + interval '1 day') - interval '1 day'
Note: This may not work on the last day of the year.
Alternatively you can use the MySQL-like functions:
select date_format(date, '%V')
This has the week starting on Sunday.

postgresql query to return the day of the week of the first day of the month two years from today

I used postgresql to solve the quesion, query to return the day of the week of the first day of the month two years from today. I was able to solve it with the query below, but I am not sure my query is correct, I just wanna make sure
select cast(date_trunc('month', current_date + interval '2 years') as date)
You are correctly computing the first day of the month two years later with:
date_trunc('month', current_date + interval '2 years')
If you want the corresponding day of the week, you can use extract();
extract(dow from date_trunc('month', current_date + interval '2 years'))
This gives you an integer value between 0 (Sunday) and 6 (Saturday)

postgresql : how to get previous month?

In my db, I have date column where the format is like 2019-04-01 or 2019-05-01 etc. If it's 2019-04-01, I'd like to get 2019-03-01 , and when it's 2019-01-01, I need to have 2018-12-01 as result.
I already did some researches however didn't find an exact solution for me.
Can anyone help me?
demo:db<>fiddle
You can use the interval addition/subtraction:
SELECT
my_date - interval '1 month'
Note, that this subtraction gives out a type timestamp. This can be casted into a date of course:
SELECT
(my_date - interval '1 month')::date AS prev_month
The solution above always subtracts one month. So, if you had 2019-04-04 you would get 2019-03-04. If you want to get the the first of previous month regardless of the current day, you can strip the day to the first of the current month before using date_trunc():
demo:db<>fiddle
SELECT
date_trunc('month', my_date)
results in 2019-04-01 for given date 2019-04-05, e.g. And with that result, you are able to subtract one month to get the first of the previous month:
SELECT
my_date,
(date_trunc('month', my_date) - interval '1 month')::date as prev_month
FROM
my_table

Get first day of last month in Java HSQL database

How can I select first day of last month and last day of next month in HSQL database?
I tried it postgres way like follows:
SELECT date_trunc('month', current_date - INTERVAL '1 month') :: DATE AS first_day_of_last_month;
Does not work in Java HSQL database thou. :(
Use the LAST_DAY(date) function.
LAST_DAY(CURRENT_DATE - 2 MONTH) + 1 DAY
LAST_DAY(CURRENT_DATE + 1 MONTH)