I need to change function of db2 to posgtgre
You don't need a FROM clause in Postgres to begin with.
The function seems to return the number of days since 0001-01-01 which can be express in Postgres as:
select current_date - date '0001-01-01';
Related
I have a postgres timescale database running in docker. For the purposes of api testing I want SELECT NOW() to return lets say 2010-12-01 23:00:44.851242 +00:00. Basically every time I start up the container I want it to think the current date is some time in December 2010.
How can I achieve this? I cant seem to find any command to set current time in postgres. Do I need to change the system time in the docker container before the database shows up? Is that even something I can do?
You can achieve this by creating a custom now() function in a separate schema and then adjusting the search_path to prefer that function over the builtin now function:
CREATE SCHEMA test;
CREATE OR REPLACE FUNCTION test.now() RETURNS timestamptz LANGUAGE SQL AS $$ SELECT '2000-01-01 0:00'::timestamptz; $$;
SET search_path TO test,pg_catalog,public;
-- make search_path change permanent for a specific user
ALTER USER <testuser> SET search_path TO test,pg_catalog,public;
SELECT now();
now
------------------------
2000-01-01 00:00:00+01
(1 row)
Time: 1.826 ms
I have used Postgres 12.3 in local development server(windows platform) and Postgres 9.2.24 in live server(Linux platform). here i am notice one problem in timestamp with time zone datatype. because, in local server select query returns data as TIME+5:30 (UTC + 5:30) but in live server return only TIME without GMT Offset (same query). so how to solve or handle this problem? please help me solve this issue.
SQL: select start_time from course_batches;
Postgres 12.3:
18:00:00+05:30
Postgres 9.2.24:
22:30:00
I got one solution to solve this issue, so I add gmt offset with actual time so my insert query like below,
INSERT INTO TABLE_NAME (start_time) VALUES ('18:00:00+05:30');
now i got correct time
17:00:00
before that i used direct time (18:00:00) string to insert record so problem was occurred in time conversion (version 12.3 08:00:00+05:30 and postgres 9.2 stored 23:00:00 )
I want to delete records older than a given (as a parameter to the function) number of days.
There are similar questions (e.g. SQL Get all records older than 30 days) which recommend to use interval '30 day' but how to make the 30 a parameter?
I have a column with a timestamp and the delete would be done in a function that's in SQL rather than plpgsql
Such an SQL function could look like this:
CREATE FUNCTION delold(integer) RETURNS void
LANGUAGE sql STRICT AS
'DELETE FROM mytable
WHERE tscol < current_timestamp
- ($1::text || '' days'')::interval';
The oracle script I'm in the process of 'converting' so it can be executed in PGAdmin4, has the following values to insert into a column of table with a data type of 'date'
to_timestamp('12-JUN-99','DD-MM-YY HH.MI.SSXFF AM')
From my understanding, FF represents Fractional Seconds.
What would be the equivalent way to represent the statement in PostgreSQL/PGAdmin 4?
SSXFF is my main concern.
I don't see how that code works in Oracle. But this should work in both Postgres and Oracle:
select to_timestamp('12-JUN-99', 'DD-MON-YY')
Do you have any idea on how to have a getdate() function in EnterpriseDB PostgreSQL? I upgraded to EDB-PSQL, and when I try to restore old data from the free PSQL, it returns error on some tables since there is no getdate().
I believe this should automatically be created upon creating new database? But it didn't. :( Only a now() function.
Can I create the function instead? Help!
If getdate() is like now() (as with SQL Server) you can simply say
create function public.getdate() returns timestamptz
stable language sql as 'select now()';