Oracle to PostgreSQL round and trunc on date columns - postgresql

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.

Related

Postgresql timestamp difference greater than 1 hour

Hi I have a entrytime and exittime timestamp in my database, how can I query it to display only ones where the person exited more than an hour later;
Select * from store where EXTRACT(EPOCH FROM (exittime - entrytime))/3600 >60
That's what I have so far but it won't work, any help would be appreciated.
Just subtract the values and compare it with an interval
Select *
from store
where exittime - entrytime > interval '1 hour';
This assumes that both columns are defined as timestamptz or timestamp

PostgresSQL: interval field value out of range

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';

Add an interval day to a date from postgresql

I would like to add a day to a date from postgreSQL, so i tried this one :
SELECT * FROM t_ask a
WHERE ask_datesend is null
AND a.ask_date_reception >= ('2019-06-12' + INTERVAL '1 day');
But it doesn't work.
But if i try that i have an answer :
SELECT * FROM t_ask a
WHERE ask_datesend is null
AND a.ask_date_reception >= '2019-06-12'
Little precision in a.ask_date_reception the date is like
"2019-06-12 15:28:01.982"
Try an explicit conversion:
a.ask_date_reception >= ('2019-06-12'::date + INTERVAL '1 day');
Or use timestamp or timestamp with time zone instead of date.

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