PostgresSQL: interval field value out of range - postgresql

I am new to PostgresSQL where I am running one query where I am looking for activities within last week but it is throwing out of range error
Postgres SQL:
select *
from myTable
where order_time > '2018-12-04 18:22:26' - INTERVAL '7 day'
Error:
IntervalFieldOverflow: ERROR: interval field value out of range: "2018-12-04 18:22:26"
Version: PostgresSQL 9.6
I have tried to resolve my issue by searching online but did not get much help.

Try casting the timestamp literal string:
select *
from myTable
where order_time > '2018-12-04 18:22:26'::timestamp - interval '7 day';

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.

How to pass a parameterized INTERVAL in a slonik/postgres query?

This query seems to be legal when I run it in Datagrip with a parameter value of '14 days'
SELECT * FROM users WHERE users.updated_at < (CURRENT_DATE - INTERVAL $1)
But trying to do something similar in slonik as below does not:
const interval='14 days'
// ...
const {rows} = await pool.query<any>(sql`
SELECT * FROM users WHERE users.updated_at < (CURRENT_DATE - INTERVAL ${interval}))
`)
Seems to yield the same query: "SELECT * FROM users WHERE updated_at < CURRENT_TIME - INTERVAL $1)" but the pool's query method throws a syntax error near "$1" error when I try to execute it.
I've tried a couple of variations including escaping the input const interval="'14 days'" and adding parens for the INTERVAL function (CURRENT_DATE - INTERVAL(${interval})) with the same error results.
Is it not possible to parameterize a slonik query this way or am I doing something stupid?
You can subtract an integer representing the number of days from CURRENT_DATE because that is a date value not a timestamp
SELECT * FROM users WHERE updated_at < CURRENT_DATE - $1
Then pass $1 as an integer
Another option is to multiply an interval of a specific length with the parameter:
SELECT * FROM users WHERE updated_at < CURRENT_DATE - (interval '1 day' * $1)
or use the make_interval() function:
SELECT * FROM users WHERE updated_at < CURRENT_DATE - make_interval(days => $1)

Oracle to PostgreSQL round and trunc on date columns

Can you please help me to convert the below Oracle query to PostgreSQL query?. Looks like "round" and "trunc" functions are not there in postgreSQL.
Here "intime" is the date and time column i.e date datatype.
select round(((intime - trunc(CURRENT_DATE-1, 'DDD')) * 24),2) from tpe
Below is the posgreSQL format and o/p -2496 days -132:00:00
SELECT (('2021-02-19 08:00:00'::timestamp AT time zone 'EST' - CURRENT_DATE - (interval '1 day'))*24)
Below is ORACLE format and o/p -2464
select ((to_date('02/19/2021 8:00:00','MM/DD/YYYY HH24:MI:SS') - trunc(CURRENT_DATE-1, 'DDD')) * 24) from dual;
And When apply round function in postgreSQL it's throwing error
SELECT round((('2021-02-19 08:00:00'::timestamp AT time zone 'EST' - CURRENT_DATE - (interval '1 day'))*24),2)
"ERROR: function round(interval, integer) does not exist.
Idea is to get same output of oracle's one.

Adding one year to a date field in postgresql

I have a table in postgresql with a field_date using the syntax 'YYYY-MM-DD', I want to add a year to the field with the the sentence:
UPDATE table SET date_field = DATEADD(YEAR, 1, date_field);
but postgres return:
ERROR: column "year" does not exist
I can't see what's wrong with the sentence
Try this:
UPDATE table SET date_field = date_field + interval '1 year'
It appears that you were trying to use SQL Server's DATEADD() function, which does not exist in Postgres.
select CURRENT_DATE, CURRENT_DATE + interval '1 year'
In your case you trying to do it in Transact SQL not in Postgres.
Try to use:
DATEADD-> "DATE + interval '1 year'"

Postgresql: Query Between time range using jsonb field

I have a table with two fields:
id(serial), data(jsonb)
And into data I have records with a Datetime field stored as UNIX timestamps:
{"Device":132,"Datetime": 1434166552,...}
I'm trying to query between ranges:
SELECT *
FROM trips
WHERE data->>'Datetime' BETWEEN
EXTRACT(EPOCH FROM date '2014-04-01') AND
EXTRACT(EPOCH FROM date '2014-04-15' + interval '1 day')
AND id = 123
Message
ERROR: operator does not exist: text >= double precision
LINE 3: WHERE data->>'Datetime' BETWEEN
Something I'm doing wrong, please cloud somebody help me? Thanks.
The ->> operator returns an JSON object field as text (see here). You need to cast it :
SELECT *
FROM trips
WHERE (data->>'Datetime')::int
BETWEEN EXTRACT(EPOCH FROM date '2014-04-01')
AND EXTRACT(EPOCH FROM date '2014-04-15' + interval '1 day')
AND id = 123