Get the start and end date by passing week number and year - postgresql

I am using EXTRACT(WEEK from doc_date) to extract the week number from a given datetime.
Is it possible to extract the start and end date by passing week number and year from Postgres?

You can add an INTERVAL of X weeks to the first date of the year, and 6 more days to get the last date.
select '2017-01-01'::date + interval '3 week' as StartWeekDate,
('2017-01-01'::date + interval '3 week') + interval '6 day' as EndWeekDate;
A reduced version:
select '2017-01-01'::date + interval '3 week' as StartWeekDate,
('2017-01-01'::date + interval '3 week 6 day') as EndWeekDate;

Related

How to group data from 26th of a month to 25th of the next month using postgresql?

I want to define the start of a “month” as the 26th day of the previous calendar month (and of course ending on 25th).
How can I group by this definition of month using date_trunc()?
This expression gives the month you want:
date_trunc(
'month',
date_add(
day,
case
when extract(day from date) > 25 then 7
else 0
end),
my_date_col
)
)
Select it and group by it.
The logic is: If the day of the month is greater than 25, then add some days to bump it into the next month before truncating it to the month.
I would use an INTERVAL to calculate the correct dates. Here an example using generate_series():
SELECT d::date as reference_date
, (d + interval '25 days')::date AS first_day
, (d + interval '1 month' + interval '24 days')::date as last_day
FROM generate_series('2020-01-01'::timestamp, '2021-01-01'::timestamp, '1 month') g(d);

date range in postgresql for a given period

I have the following records in a table, as image below. The last period is December / 2019.
I would like to list the periods within a range of 2 years (backwards) from the current date.
For example: today 09/10/2019, list periods from 01/01/2017 to 12/12/2019
I have difficulty assembling the query below.
SELECT c_period_id, name, startdate, enddate
FROM adempiere.C_Period
WHERE startdate BETWEEN now() - INTERVAL '2 year' AND now()
order by startdate desc
I am not quite sure what your problem is, but if it is rounding the dates to the year boundary, this might serve:
WHERE startdate >= date_trunc('year', current_timestamp) - INTERVAL '2 years'
AND startdate < date_trunc('year', current_timestamp) + INTERVAL '1 year'

Get first date of previous month

Is there any way to get the first day of previous month other than
date_trunc('month', current_date - interval '1 month') ?
I'm trying to save a query in a Report Designer software (DBxtra) but the software freezes while using the "interval" feature of PostgreSQL.
You can try to calculate the previous month manually.
One approach is extract the month and subtract 1 if it is not 12(in this case you return 1):
SELECT to_timestamp(concat(EXTRACT(YEAR from current_date), '-',CASE (EXTRACT(MONTH from current_date)) WHEN 12 THEN 1 ELSE (EXTRACT(MONTH from current_date)-1) END,'-', 1), 'YYYY-MM-DD');
If you need it without timezone:
SELECT to_timestamp(concat(EXTRACT(YEAR from current_date), '-',CASE (EXTRACT(MONTH from current_date)) WHEN 12 THEN 1 ELSE (EXTRACT(MONTH from current_date)-1) END,'-', 1), 'YYYY-MM-DD')::timestamp without time zone;
Try:
make_interval(month := 1)
'1 month'::interval
cast('1 month' as interval)
instead of interval '1 month'

How to calculate end of the month in Postgres?

How to calculate end of the month in Postgres? I have table with column date datatype. I want to calculate end of the month of every date. For Eg. In the table there values like "2015-07-10 17:52:51","2015-05-30 11:30:19" then end of the month should be like 31 July 2015,31 May 2015.
Please guide me in this.
How about truncating to the beginning of this month, jumping forward one month, then back one day?
=# select (date_trunc('month', now()) + interval '1 month - 1 day')::date;
date
------------
2015-07-31
(1 row)
Change now() to your date variable, which must be a timestamp, per the docs. You can then manipulate this output (with strftime, etc.) to any format you need.
Source
SELECT TO_CHAR(
DATE_TRUNC('month', CURRENT_DATE)
+ INTERVAL '1 month'
- INTERVAL '1 day',
'YYYY-MM-DD HH-MM-SS'
) endOfTheMonth
Hi I tried like this and it worked
Date(to_char(date_trunc('month'::text, msm013.msa011) + '1 mon - 1 day '::interval , 'DD-MON-YYYY') )
Thanks a lot!!

get last three month records from table

How to get last 3 months records from the table.
SELECT *
from table
where month > CURRENT_DATE-120
and month < CURRENT_DATE
order by month;
I have used the above query is it correct? shall I use this for get last 3 month record from the table.
You can use built-in INTERVAL instruction
Check how this works:
SELECT CURRENT_DATE - INTERVAL '3 months'
and you can rewrite your SQL to:
SELECT * from table where date > CURRENT_DATE - INTERVAL '3 months'
(not checked but this should give you an idea how to use INTERVAL instruction)
Try that:
SELECT *
FROM table
WHERE month BETWEEN EXTRACT(MONTH FROM NOW() - INTERVAL '3 months')
AND EXTRACT(MONTH FROM NOW())
ORDER BY month
;
This filters the last 3 calendar months
SELECT * from table where date >= to_char(CURRENT_DATE - INTERVAL '3 months', 'YYYY-MM-01')::date
select date::date
from generate_series((current_date - INTERVAL '1 Month')::date, (current_date - INTERVAL '1 DAY')::date,'1
day'::interval) date
WHERE date >= date_trunc('month', current_date - interval '3' month)
and date < date_trunc('month', current_date)
This will give last three months date list, excluding current months date. Example if current month is November. This list will give use all dates of August, Septemeber and October.