How to use oracle sys_sxtract_utc like function in postgresql? - postgresql

I have an oracle query that uses sys_sxtract_utc function in where clause:
select * from my_table where sys_sxtract_utc(timestmp) > sys_sxtract_utc(last_stable_date)
But I could not use this in postgresql query. I could not find any function like this.
How can I use in postgresql?

You can use at time zone in your query to get the current GMT timestamp
select CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
PostgreSQL Community Doc

Related

Having a problem finding those records between yesterday and today based on a UTC column value using PostgreSQL

Using PostgreSQL and trying to query a table that contains a UTC column. The UTC column is a column without the timezone as I understand it from the developer. Example in my DB for a record is (2021-08-26 13:59:26.867578). And I have to search for records that are between yesterday's date and today's date. When I tried the SQL statement below I get this error:
[42883] ERROR: operator does not exist: timestamp without time zone - integer Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Position: 63
Here is my SQL statement for PostgreSQL:
SELECT omd.*
FROM "OCRMetaDatas" omd
WHERE (omd."ScannedAt")-5 BETWEEN Now()-1 and now()
ORDER BY omd."ScannedAt" desc;
Any help/direction would be appreciated. Thanks.
You can't operate integer with datetimes. Here you are trying to do that twice:
(omd."ScannedAt")-5
and
Now()-1
You should use INTERVAL with datetime there, such as:
SELECT omd.*
FROM "OCRMetaDatas" omd
WHERE (omd."ScannedAt")- '5 days'::INTERVAL BETWEEN Now()- '1 day'::INTERVAL and now()
ORDER BY omd."ScannedAt" desc;
As illustration of how to use at time zone:
--My TimeZone
show TimeZone;
TimeZone
------------
US/Pacific
select '2021-08-26 13:59:26.867578'::timestamp;
timestamp
----------------------------
2021-08-26 13:59:26.867578
select '2021-08-26 13:59:26.867578'::timestamp at time zone 'UTC';
timezone
-------------------------------
2021-08-26 06:59:26.867578-07
select now();
now
-------------------------------
2021-08-26 09:23:57.818477-07
at time zone will normalize the timestamps to the same time zone.

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.

extract timestamp from EPOCH cloumn from postgresql DB table

I am trying to execute below-mentioned PostgreSQL query and in which checkin_ts in epoch format and want to write a query which is humanly understandable(putting timestamp in human readable format).
select * from users where to_timestamp(checkin_ts) >= '2017-11-11 00:00:00'
LIMIT 100;
when I tried to execute the above query then I get the following error
ERROR: execute cannot be used while an asynchronous query is underway
You need to give us the description of your table, to know checkin_ts format.
But to_timestamp need to text paramaters like this :
SELECT *
FROM users
WHERE to_timestamp(checkin_ts::text, 'YYYY-MM-DD HH24:MI:SS') >= '2017-11-11 00:00:00' LIMIT 100;
And what is your environment to execute this query ?
The error seems to be that you are trying to execute two queries on the same connection using two different cursors.

redshift datediff not working when current_timestamp is used but working when getdate() function is used

I am querying a timestamp column with datediff from current_timestamp. But it gives error.
DATEDIFF(minute, timestamp_field ,current_timestamp::TIMESTAMP)
or
DATEDIFF(minute, timestamp_field ,current_timestamp)
DataType of timestamp_field is "TIMESTAMP DEFAULT '2016-03-29 20:33:33.404256'::timestamp without time zone"
OutPut:
ERROR: Specified types or functions (one per INFO message) not
supported on Redshift tables.
Warnings: Function ""timestamp"(timestamp with time zone)" not
supported. Function "timestamptz(timestamp with time zone,integer)"
not supported. Function "timestamptz(text)" not supported.
But following query is working if I use getdate() function
DATEDIFF(minute, timestamp_field ,getdate()::TIMESTAMP)
I just simply casted the timestamp with timezone to timestamp and it was working.
example:
select datediff(min, CURRENT_TIMESTAMP::timestamp, CURRENT_TIMESTAMP::timestamp);
This worked for me:
datediff('day', ref_date::date, actual_date::date)

Postgresql How extract date

I need get only the date from now() at my time zone, I have this query:
SELECT now() AT TIME ZONE 'America/Santiago'
And I'm getting something like this "2015-06-08 23:59:34.142569"
but I need extract only the date, how can I get it?
Thanks.
If you want the server's date,
SELECT current_date;
If you need the date for any timestamp, eg the one you've gotten into your timezone, use date().
SELECT date(now() AT TIME ZONE 'America/Santiago');
Docs: http://www.postgresql.org/docs/current/static/functions-datetime.html
For postgres you want
select current_date;
if you need to extract any of those fields out of the returned value you can use extract
EXTRACT (field FROM source)