Function to_timestamp and BC in time - postgresql

I try convert string value of time to timestamp
declare _query timestamp;
_query := to_timestamp('12:12:12', 'hh24:mi:ss');
And when I try output value of _query they returns
0001-01-01 12:12:12.000000 BC
Why they add the first year of BC ?

Related

passing variable date with fixed time to a function

I am trying to create a function which accepts two arrays, and a date. The function uses the date
in a way where I want hardcoded values of time (with timezone) which are already stated in the function body (in the orig_dataset CTE). Here is my function so far:
CREATE or replace FUNCTION f_loop_in_lockstep_final(_id_arr int[], _counter_arr int[], d_date date)
RETURNS TABLE (uc_name_ varchar)
LANGUAGE plpgsql AS
$func$
DECLARE
_id int;
_counter int;
d_date date;
BEGIN
FOR _id, _counter IN
SELECT *
FROM unnest (_id_arr, _counter_arr) t
LOOP
RETURN QUERY
with orig_dataset as (
select routes
from campaign_routes cr
where cr.created_at between 'd_date 06:00:00 +05:00' and 'd_date 18:00:00 +05:00'
)
-- a couple of further CTE's result in a final CTE called final_cte
select * from final_cte;
END LOOP;
END
$func$;
When I use the following function call:
SELECT * FROM f_loop_in_lockstep_final('{454,454}'::int[]
, '{2,3}'::int[], to_date('2023-01-17','YYYY-MM-DD'));
I receive the following error:
SQL Error [22007]: ERROR: invalid input syntax for type timestamp with time zone: "d_date 06:00:00 +05:00"
Where: PL/pgSQL function f_loop_in_lockstep_final(integer[],integer[],date) line 14 at RETURN QUERY
Well, obviously 'd_date 06:00:00 +05:00' is not a valid date literal.
You need to add a time value to the variable to create a timestamp value based on that:
where cr.created_at between d_date + '06:00:00 +05:00'::time
and d_date + '18:00:00 +05:00'::time
I am not entirely sure that using a time zone offset in a time constant works correctly, so maybe you need:
where cr.created_at between ((d_date + '06:00:00'::time) at time zone '+05:00')
and ((d_date + '18:00:00'::time) at time zone '+05:00')

Convert from Oracle function to postgreSQL function

Can you please help to convert below Oracle function to PostgreSQL function?
CREATE OR REPLACE FUNCTION CONVERT_TIME (DATETIME IN TIMESTAMP, TZ1 IN VARCHAR2, TZ2 IN VARCHAR2)
RETURN DATE
AS
RETVAL DATE;
BEGIN
RETVAL := FROM_TZ(TO_TIMESTAMP(TO_CHAR(DATETIME,'YYYY-MM-DD HH:MI:SS AM'),'YYYY-MM-DD HH:MI:SS AM'), TZ_OFFSET (TZ1)) AT TIME ZONE TZ_OFFSET (TZ2);
RETURN RETVAL;
END;
/

Data arguments passing to user-defined function postgresql

I'm trying to create user-defined function which will return SUM from expenses view based on given dates.
CREATE FUNCTION budget.getTotalAmountFromView (startDate DATE, endDate DATE)
RETURNS DECIMAL AS $$
DECLARE
totalValue DECIMAL := 0;
BEGIN
SELECT INTO totalValue sum(amount) from budget.epenses_overview where transaction_date >= startDate AND transaction_date <= endDate;
RETURN totalValue;
END;
$$ LANGUAGE plpgsql;
I am trying to invoke it using:
SELECT * FROM budget.getTotalAmountFromView(TO_DATE(20190201, YYYYMMDD), TO_DATE(20190225, YYYYMMDD));
But it returns error
AFTER CHANGES:
Function shall be assigned to the right schema -> budget;
and invoke:
SELECT budget.getTotalAmountFromView('20190201'::DATE, '20190225'::DATE);
You don't need FROM for scalar function:
SELECT budget.getTotalAmountFromView22('20190201'::DATE, '20190225'::DATE);
You are lacking single quotes around the date and format strings.
TO_DATE(20190201, YYYYMMDD) should be TO_DATE('20190201', 'YYYYMMDD')

Postgres invalid date format

Below are some samples of date from a column in my database. Currently, these date values are stored in varchar and I am planning to set these value as timestamp in another table.
How can I set a default rules to cast all the invalid date format to a NULL value or some sort of '00/00/0000 00:00:00" format?
pickup_time
01/01/2016 07:35:00
00:00:00
31/12/2015
07:35:00
Expectation:
pickup_time
01/01/2016 07:35:00
NULL
31/12/2015 00:00:00
00/00/0000 07:35:00
Ok, here is the basic idea, so you need to set a if/else with regex (or something to figure out the pattern of the string) per each case you want to handle.
create or replace function my_super_converter_to_timestamp(arg text)
returns timestamp language plpgsql
as $$
begin
begin
-- if(arg like '00:00:00') do something ...
return arg::timestamp;
exception when others then
return null;
end;
end $$;

How to convert a text variable into a timestamp in postgreSQL?

I know how to convert a text to timestamp in postgreSQL using
SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY')
but how can I convert a text variable (inside a function) to timestamp??
In my table (table_ebscb_spa_log04) "time" is a character varying column, in which I have placed a formated date time (15-11-30 11:59:59.999 PM).
I have tried this function, in order to convert put the date time text into a variable (it always change) and convert it into timestamp...
CREATE OR REPLACE FUNCTION timediff()
RETURNS trigger AS
$BODY$
DECLARE
timeascharvar character varying;
timeastistamp timestamp;
BEGIN
IF NEW.time_type = 'Lap' THEN
SELECT t.time FROM table_ebscb_spa_log04 t INTO timeascharvar;
SELECT to_timestamp('timeascharvar', 'yy-mm-dd HH24:MI:SS.MS') INTO timeastistamp;
END IF;
RETURN timeastistamp;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION timediff()
OWNER TO postgres;
but whenever I run it in the table, it shows this ERROR message...
It seems that "to_timestamp" waits for a number to be the year, how can I get it to recognize the variable as if it were numbers?
The first parameter to to_timestamp should be your var not a string containing the name of your var:
to_timestamp(timeascharvar, 'yy-mm-dd HH24:MI:SS.MS')