postgresql : how to get previous month? - sql

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

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.

Is there a way to add in column for Week number depending on a date column?

I was wondering if it is possible in SQL to read in a date column and based on that date create a new column and automatically have the Week number as well. For example today is 4/7/2020 , so the query would have Week 15 populated for that?
]1
In the picture the week column would ideally be populated beside 'datestr'.
Thank you]2
In redshift, you can use date_part() with the w specifier to get the week number of a date or timestamp:
select t.*, date_part(w, datestr) week_number from mytable t
Note that weeks starts on Monday in Redshift. If you want the week to start on Sunday:
select t.*, date_part(w, datestr + interval '1' day) week_number from mytable t
You could use extract. I am not 100% sure if weeks in Redshift start from Sunday or Monday, but you can adjust the interval to test the edge cases.
select datestr, extract(week from datestr + interval '1 day') as weeknum
from your_table

EOMONTH equivalent in Postgresql

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'

SQL Count days till first of the month

How would I count the days from a date till the first of the following month
Example:
--Start Date
07-07-2011
How many days till:
-- The 1st of the succeeding month of the start date above
08-01-2011
Expected Result (in days):
25
So if I counted the day I get 25, so running this query gets me the desired timestamp:
SELECT CURRENT_DATE + INTERVAL '25 DAYS'
Results:
2011-08-01 00:00:00
just can't think of a way to get the number of days, any suggestions?
Or start date, end date, number of days between?
I don't have a PostgreSQL server handy, so this is untested, but I would try:
SELECT (DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 MONTH') - CURRENT_DATE