Calculating a date in Postgres by adding months? - postgresql

I have a postgres table that has the following fields
start_date,duration
duration contains any number of months, so to calculate the end date you add the months in duration to the start date. Now I want to do something like this with a postgres query.
SELECT *
FROM table
WHERE start_date > '2010-05-12'
AND (start_date + duration) < '2010-05-12'
Is this possible and how does one right the syntax?
The version of my postgres is PostgreSQL 8.1.22 on x86_64-redhat-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)

try:
(start_date + (duration * '1 month'::INTERVAL)) < '2010-05-12'
or
(start_date + (duration || ' month')::INTERVAL) < '2010-05-12'
More info: Date/Time Functions and Operators

You might find this article useful: PostgreSQL: month := interval '30 days';
Also this link: http://wiki.postgresql.org/wiki/Working_with_Dates_and_Times_in_PostgreSQL, more specifically the section called "WORKING with DATETIME, DATE, and INTERVAL VALUES".

Related

How to migrate "as of timestamp" query in PostgreSQL

I want to migrate or write an equivalent query to get the data from table one hr before the current time in PostgreSQL.
oracle query:
select *
from T_DATA as of timestamp (systimestamp - interval '60' minute);
select * from T_DATA where timestamp_column >= now() - interval '1 hour'
Since flashback queries are not supported in postgresql, One approach I tried with temporal tables extension.

Yesterday - in Redshift & PostgreSQL - date addition compatibility

Attempting to write queries that will be compatible with both PostgreSQL and Amazon Redshift.
Reason: Syncing data from PG to RS to perform complex queries, but in dev/QA environments budget (and DB size) dictates to stay with PG only.
Request: return yesterday's date
In PostgreSQL:
SELECT DATE((NOW() - '1 DAY'::INTERVAL));
In Redshift:
SELECT DATE(DATEADD(DAY, -1, GETDATE()));
Problem: Neither works in the other platform.
Is there a compatible way to achieve requested action?
ORM is an option we'd like to avoid.
The following works in Postgres and Redshift:
ANSI standard SQL:
SELECT current_date - interval '1' day;
-- 2018-06-19 00:00:00
SELECT current_timestamp - interval '1' day;
-- 2018-06-19 13:40:06.509337+00
Postgres (and I believe Redshift as well) also supports the alternative (non-standard) interval syntax: interval '1 day'
Or more compact (not 100% ANSI SQL but works in both):
SELECT current_date - 1;
-- 2018-06-19 00:00:00
SELECT current_timestamp - 1;
-- 2018-06-19 13:40:06.509337+00

Using BETWEEN or comparison operators in PostgreSQL

I'm having trouble with using the between operator in where clause. I have the following queries:
SELECT *
FROM table
WHERE timestamp_column BETWEEN (current_date - interval '1 day')::date
AND current_date => 500k rows
SELECT *
FROM table
WHERE timestamp_column >= (current_date - interval '1 day')::date => 1 mil rows
Does anyone have any idea why the result set is different? Shouldn't it be the same? I am trying to compare the number of rows from a database in PostgreSQL with data from Sybase ASE. The same 2 queries ran in Sybase give the same results. The expected result set is 1 milion rows. This should be the number of rows that I have between yesterday and today.
PostgreSQL v9.5, timestamp_column = timestamp without time zone
I tried using now(), current_timestamp but same result.
What am I missing here? If I didn't made myself clear let me know.

last_day in PostgreSQL

The oracle last_day function return the last day of the month.
example:
nls_date_format='YYYY-MM-DD H24:MI:SS'
select last_day(sysdate) from dual;
LAST_DAY(SYSDATE)
-------------------
2014-06-30 15:45:43
oracle return the time value as well.
I have tried below sql in PostgreSQL which return the last day of month but time value is "00:00:00".
select (date_trunc('month', now()) + interval '1 month -1 day')::timestamp(0);
?column?
---------------------------
2014-06-30 00:00:00
(1 row)
the sql return the date correctly but I want date and time like oracle.
select (
date_trunc('month', now())
+ interval '1 month -1 day'
+ now()::time
)::timestamp(0);
The function last_day() should do the trick. This function should be found in a extenstion package called "orafce". Containing other functions used in Oracle.
Install the correct version of orafce package (if needed):
apt-get install postgresql-9.1-orafce
SQL:
Create Extension orafce
If error rename orafce--3.0.sql to orafce--3.03.sql or user later version of orafce.
Use the function:
SELECT last_day('2015-01-01')
Works like expected and the result is '2015-01-31'

records from yesterday in postrgesql

I have an application in which I've used MySQL. I had a report that stretched records of the last 24 hours. I used the query:
WHERE (DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= FROM_UNIXTIME(`workorder`.`CREATEDTIME` / 1000))
Now I have to use PostgreSQL and do not know how to make a report of the last 24 hours.
Can any of you help?
WHERE workorder.createdtime > current_date - 1 -- Yesterday and today
WHERE workorder.createdtime > current_timestamp - interval '1 day' -- last 24hr
> TIMESTAMP 'yesterday'
For convenience, Postgres includes a few hard-coded values as special Date/Time inputs. They include:
yesterday
today
tomorrow
now
Try SELECT TIMESTAMP 'now'.
For example, here is a query.
SELECT when_row_created_
FROM customer_
WHERE when_row_created_ > TIMESTAMP 'yesterday'
ORDER BY when_row_created_ DESC
;
These commands may not be appropriate to production code, but they certainly are handy in development. Read the docs and do some practice to be sure you understand the behavior of these commands, how the session’s time zone affects them and so on.
Downsides include (a) implicitly ignoring the crucial issue of time zone, and (b) not standard SQL.
where workorder.createdtime >= now() - interval '24 hour'
WHERE workorder.createdtime::date = current_date - 1; --from yesterday