Checking timestamps in PL/pgSQL - sql

If I have a function in PL/pgSQL that takes in a timestamp, what is the best way to identify whether that date is less than 12 months in the past?
e.g.
CREATE FUNCTION do_something(foo timestamp) ....
-- IF foo is less than 12 months in the past THEN
-- do something
-- END IF;
END;

Read about intervals on PostgreSQL doc: Date Types. Use something like:
where foo < CURRENT_TIMESTAMP - interval '12 months'

Or, equivalently: age(foo) < interval '12 months'

Related

Add one day to Now() and return as default value in SQL query

I am attempting to add a day to NOW() and return as the values for a column.
This works
SELECT NOW() as date
But this gives an error
SELECT DATE_ADD( NOW(), INTERVAL 1 DAY) as date
Is there a way to achieve this in a postgres query?
Thanks
I don't think there's a date_add() function in PostgreSQL:
ERROR: function date_add(timestamp with time zone, interval) does not
exist
LINE 1: select date_add(now(), interval '1 day');
^
HINT: No function matches the given name and argument types. You
might need to add explicit type casts.
but you can use a regular + operator to add an interval to timestamptz that's returned by now(). Demo:
select now() + '1 day'::interval;
You can define that function for convenience:
create function date_add(arg1 timestamptz, arg2 interval)
returns timestamptz language sql as $$
select arg1+arg2
$$;
select date_add(now(), interval '1 day') as date;
-- date
---------------------------------
-- 2022-11-29 12:28:12.393508+00
But I don't think it's really more convenient than the operator. You'd also have to overload it to make sure how it deals with different types - you can see in the demo how by default PostgreSQL will try to guess and cast automatically.

how to replace date in postgres with a static number

I am writing a Postgres procedure and I want to replace only date part of date with some static no.
eg:-
varDate Date default '2018-05-21';
Say I want to make this date as '2018-05-08';
Can anyone tell how to achieve this.
Till now what i have tried is this
varDate := varDate - interval '1 day' * 21 + interval '1 day' * 8;
The above expression gives me proper results. But is there any shortcut to change only the date part of the date.
As far as I understand you want to change the day of the month to 8.
One way to do this is to "truncate" the date to the start of the month, then add 8 days:
vardate := date_trunc('month', vardate)::date + 8;
date_trunc returns a timestamp that's why the cast ::date is needed.
Another option is to "build" a date based on the existing date:
vardate := make_date(extract(year from vardate)::int, extract(month from vardate)::int, 8);
Another option is to add a number of days to the date so you land on the 8th day:
select vardate::date + (8 - extract(day from vardate) * interval '1 day'

Oracle SQL : Value will be true every after 30 days

I'm not really good in Oracle SQL but i'm trying to check a date something like this
For example: 01-Jan-2000
How can I check if 30 days passed already, and also for 60 months and so on.
Basically i need to make a condition that will return true after a month and so on. i can check it 1 by 1 but it's obviously not good.
DECLARE
appn_date DATE;
BEGIN
appn_date := Trunc(TO_DATE ('10-AUG-2016'));
IF (appn_date = trunc(sysdate) - interval '30' day) THEN
DBMS_OUTPUT.PUT_LINE('Yes');
ELSE
DBMS_OUTPUT.PUT_LINE('No');
END IF;
END;
It should output Yes because 30 days passed already, but it will also output Yes after 60 days, 90 days and so on. I think modulo will work on this. any idea?
To check if that column's value is older then 30 days, use:
where the_date_column <= sysdate - interval '30' day
or a bit shorter but identical
where the_date_column <= sysdate - 30
For months you need to use add_months()
where the_date_column <= add_months(sysdate, -60);
As Oracle's DATE isn't really a "date" but a "datetime" you might need trunc() (for both: the date column and sysdate) if you don't want to consider the time part.
See the manual for details on how to specify intervals and the available date functions

Postgresql current datetime for past year

Is there a function on postgresql that will let me get the current datetime for the past year or x number of past years?
I know i can do this select now() - interval '1 year'; but in a function how can i put the number of years in a variable
x := '2 year'
Is it possible to do this select now() - interval x;
I tried but it give me error
If you want to use variable you can do this:
CREATE OR REPLACE FUNCTION func(input integer)
RETURNS TIMESTAMP WITHOUT TIME ZONE AS
$BODY$
declare
result TIMESTAMP WITHOUT TIME ZONE;
begin
select now() - (input || ' years')::interval into result;
return result;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
For the date, you would use:
select current_date - interval '1 year'
For the date/time:
select now() - interval '1 year'

How to list records with date from the last 10 days?

SELECT Table.date FROM Table WHERE date > current_date - 10;
Does this work on PostgreSQL?
Yes this does work in PostgreSQL (assuming the column "date" is of datatype date)
Why don't you just try it?
The standard ANSI SQL format would be:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10' day;
I prefer that format as it makes things easier to read (but it is the same as current_date - 10).
http://www.postgresql.org/docs/current/static/functions-datetime.html shows operators you can use for working with dates and times (and intervals).
So you want
SELECT "date"
FROM "Table"
WHERE "date" > (CURRENT_DATE - INTERVAL '10 days');
The operators/functions above are documented in detail:
CURRENT_DATE
INTERVAL '10 days'
My understanding from my testing (and the PostgreSQL dox) is that the quotes need to be done differently from the other answers, and should also include "day" like this:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10 day';
Demonstrated here (you should be able to run this on any Postgres db):
SELECT DISTINCT current_date,
current_date - interval '10' day,
current_date - interval '10 days'
FROM pg_language;
Result:
2013-03-01 2013-03-01 00:00:00 2013-02-19 00:00:00
The suggested answers already seem to solve the questions. But as an addition I am suggesting to use the NOW() function of PostgreSQL.
SELECT Table.date
FROM Table
WHERE date > now() - interval '10' day;
Additionally you can even specifiy the time zone which can be really handy.
NOW () AT TIME ZONE 'Europe/Paris'
Starting with Postgres 9.4 you can use the AGE function:
SELECT Table.date FROM Table WHERE AGE(Table.date) <= INTERVAL '10 day';
Just generalising the query if you want to work with any given date instead of current date:
SELECT Table.date
FROM Table
WHERE Table.date > '2020-01-01'::date - interval '10 day'
I would check datatypes.
current_date has "date" datatype, 10 is a number, and Table.date - you need to look at your table.
you can use between too:
SELECT Table.date
FROM Table
WHERE date between current_date and current_date - interval '10 day';