PostgreSQL Extract Year from Timestamp and then apply Interval Calculation - postgresql

I'd like to filter a dataset to include everything with a created_date >= current year plus 5 full years prior. If I ran this today, I'd want anything with a created_date >= 01/01/2016 (2021 - 5 = 2016)
where extract(year from created_date) >= extract(year from current_date) - interval '5 years'
of course this doesn't work with double precision datatype. Any thoughts? Thank you.

Use date_trunc() to "round" the dates to the first of January.
where date_trunc('year', created_date) >= date_trunc('year', current_timestamp) - interval '5 years'
But the "I'd want anything with a created_date >= 01/01/2016" seems to indicate you might want:
where created_date >= date_trunc('year', current_timestamp) - interval '5 years'

Related

current_date in redshift exclude today's date when i am using with between command

I want to query data for last 30 days including today from redshift table. below is my query.
my date_column's type is 'timestamp without timezone'
select *
from mytable
WHERE date_column BETWEEN current_date - INTERVAL '30 day' AND current_date
order by date_column desc;
It gives the result for 30 days. But it doesn't include today's result.
I want to query for 30 days result including today's result also.
If it's a timestamp don't use between as it also compares the time part. Use a range query:
where date_column >= current_date - interval '30 day'
and date_column < current_date + interval '1 day'
Note that the upper bound is using < together with "tomorrow"
With Postgres this could be simplified to
where date_column >= current_date - 30
and date_column < current_date + 1
but Redshift isn't Postgres and I don't know if that would work there.

PostgreSQL: How to get last 2 years of data from today's date

I am using PostgreSQL and need to know how to get data from last 2 years starting today, which is current_date.
Is my query correct ?
select count(*) from table_name where
creation_date >= date_trunc('year', now()) - interval '2' year and
creation_date < date_trunc('year', now());
Assuming you want a two year span starting from the current date:
select count(*) from table_name where
creation_date >= current_date - interval '2' year and
creation_date < current_date;
Using current_date eliminates the need to truncate now().
What About Extracting only Year from the two parts and make just little comparaison like that;
select count(*) from table_name where
EXTRACT(YEAR FROM CURRENT_DATE) - EXTRACT(YEAR FROM creation_date) BETWEEN 0 and 2

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'

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.