Subtracting numeric col from now() - plpgsql

I have a query which is working fine in oracle using sysdate. The same query failing in postgres when i use now(). Please suggest me a workaround.
oracle
select SYSDATE-(SELECT TO_NUMBER(c_value)/24
FROM t_con
WHERE con_key = 'WIRELESS');
the result is like this:
11/10/2022 10:29:19 AM
Postgres:
(select(CURRENT_DATE-(select to_number(config_value)/24)::INT)::TIMESTAMP FROM tvam_configs WHERE config_key = 'TVAM_WIRELESS_PRICING')
gave me value--[5:49 PM] VOLETI, ARUNA (LABOR)
"2022-11-15 00:00:00"
how do i get the same value that i get in oracle.
I tried all the ways that you suggested in the comments.
last_modified which sis a timestamp column.
where last_modified>(select(CURRENT_DATE-(select to_number(config_value)/24)::INT)::TIMESTAMP FROM tvam_configs WHERE config_key = 'TVAM_WIRELESS_PRICING')

Firstly, on PostgreSQL to_number(string) - this is incorrect syntax, correct syntax is: to_number(string, format). For more information: PostgreSQL Data Type Formatting Functions
And on PostgreSQL you can use only these operations:
date + integer → date
date + interval → timestamp
date + time → timestamp
interval + interval → interval
timestamp + interval → timestamp
time + interval → time
date - date → integer
date - integer → date
date - interval → timestamp
time - time → interval
time - interval → time
timestamp - interval → timestamp
interval - interval → interval
timestamp - timestamp → interval
interval * double precision → interval
interval / double precision → interval
Function now() returns timestamp, so you can not use now() - 12
But, you can use now()::date - 12
If you need operations with only type timestamp, you can use interval types, for example:
select now() + interval '12 days'
or
select now() - interval '1 months'
or
select now() - interval '2 hours'

Related

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.

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

Last 7 days from postgres table epoch timestamp

I have a table (funny_quotes) which has a column called quote_date and it stores data in unix epoch time (seconds)
I want to run a query to only return data from this table(funny_quotes) only from the last 3 days.
I created a table locally in postgres but unable to store dates as epoch time, they only get stored as timestamp values
select * from funny_quotes where quote_date > (now() - interval '3 days')
You should modify the right side of the equation to epoch in order to be able to compare:
select * from funny_quotes where quote_date > extract(epoch from (now() - interval '3 days'))
Or the other way around: convert quote_date into timestamp:
select * from funny_quotes where to_timestamp(quote_date) > now() - interval '3 days'
You can read more about it in the docs

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.

Ignoring seconds from timestamp postgres

I am running a cron job every 1 minute for notifying users for events
select * from events where event_start = now() - interval '30 minutes'
so that I can send the users a notification prior to 30 mins of event
problem is event start is a timestamp field so if there is a difference in seconds it this wll not work ,so how can ignore the seconds part .
You can use date_trunc() to remove the seconds:
select *
from events
where event_start = date_trunc('second', now()) - interval '30' minutes
More details in the manual: http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
In order to ignore seconds, you can use date_trunc() function.
The function date_trunc is conceptually similar to the trunc function
for numbers.
date_trunc(field, source [, time_zone ]) source is a value expression
of type timestamp, timestamp with time zone, or interval. (Values of
type date and time are cast automatically to timestamp or interval,
respectively.) field selects to which precision to truncate the input
value. The return value is likewise of type timestamp, timestamp with
time zone, or interval, and it has all fields that are less
significant than the selected one set to zero (or one, for day and
month).
Valid values for field are:
microseconds
milliseconds
second
minute
hour
day
week
month
quarter
year
decade
century
millennium
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-01-01 00:00:00
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
Result: 2001-02-16 00:00:00-05
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
Result: 2001-02-16 08:00:00-05
SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
Result: 3 days 02:00:00
So in your case, you should use:
SELECT *
FROM events
WHERE event_start = date_trunc('minute', now()) - INTERVAL '30' MINUTE;