I am trying to do something similar to below MSSQL code in postgresql but i am getting error.
MSSQL code
IF (YEAR(GETDATE()) ='2020')
SELECT 1
ELSE SELECT 2
Postgres CODE
CREATE PROCEDURE PROCNAME(
DATEVAR timestamp
)
AS $BODY$
DECLARE
BEGIN
if (Extract (YEAR FROM TIMESTAMP DATEVAR) ='2020') THEN
DO SOMETHING;
ELSE DO SOMETHING ELSE;
END IF ;
$BODY$;
ERROR: syntax error at or near "DATEVAR"
Remove the timestamp prefix it is only required if you want to refer to a constant.
And numbers should be compared to numbers, not to strings.
CREATE PROCEDURE PROCNAME(
DATEVAR timestamp
)
AS $BODY$
DECLARE
BEGIN
if Extract (YEAR FROM DATEVAR) = 2020 THEN
DO SOMETHING;
ELSE DO SOMETHING ELSE;
END IF;
$BODY$;
Related
I have written a stored procedure as below but I am not able to dynamically append today's date into my table name. The below code gives me an error "Invalid Operation: syntax error" while calling the stored procedure. Thank you in advance
CREATE OR REPLACE PROCEDURE schema.reftable (vname date)
AS $$
DECLARE tname VARCHAR(50):= 'schema.MT_'||QUOTE_LITERAL(vname) ;
BEGIN
execute
'create table ' || tname || ' as select current_date as upload_date, * from schema.test';
END;
$$ LANGUAGE plpgsql
;
-- Calling the stored procedure
call schema.reftable(current_date);
In one of my functions in Postgres, I am trying to loop over a range of dates using the following code:
FOR timesheet_date IN select generate_series('2012-11-24'::date,'2012-12-03','1 day'::interval)::date LOOP
//My code goes here
END LOOP;
But I am getting an error
Now as am getting dates, I think it is not a record variable and hence the error.
But, how can I loop through a date range ? I am very new to Postgres actually.
DO $$
declare
dt record;
begin
FOR dt IN SELECT generate_series('2023-02-10'::date, '2023-02-15'::date, '1 day'::interval) LOOP
RAISE NOTICE 'Processing date: %', dt.generate_series;
END LOOP;
end; $$
I created a function to get me a value of the current date in yyyymmdd format.
create or replace function currdate() returns text as $$ select 'account_'||TO_CHAR(current_date , 'yyyymmdd');
$$ LANGUAGE sql;
Now i have to write a Proc to append this date 'yyyymmdd' to a create a snapshot table that will be created on the 1st and last day of the month.
I wrote the below Proc
create or replace procedure proc_1() AS $$
declare
dttoday text := currdate();
int_check int := checkint();
begin
if int_check = 1 then
create table **schema.snapshot_currdate** as (select * from schema1.original_table); end if;
end;
$$
language plpgsql;
Here when the table is created : snapshot_currdate I need it to be created as name snapshot_yyyymmdd (Date it was executed)
Declared Variable int_check is a function that checks if its 1st or last day of the month it will return integer value 1
Any Ideas?
FYI, Postgres 9.1 is four years past EOL.
Something like:
currdate() returns text as $$ select 'account_'||TO_CHAR(current_date , 'yyyymmdd');
$$ LANGUAGE sql;
CREATE OR REPLACE PROCEDURE public.proc_1()
LANGUAGE plpgsql
AS $procedure$
declare
dttoday text := currdate();
begin
EXECUTE 'create table '|| quote_ident('public')||'.'||quote_ident('snapshot_'||split_part(dttoday, '_', 2))||'()';
RAISE NOTICE '%', dttoday;
end;
$procedure$
The split_part is because currdate() returns account_yyyymmdd and you say you want snapshot_yyyymmdd. For more information on dynamic commands see here:
https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
In the create screen of the Postgress GUI I'm getting a syntax error at the while statement. Language is plpgsql and return type is void. TIA
enter code here
begin
declare counter integer := 1;
declare CurrentDate Date := '1/1/2018';
while CurrentDate < '1/1/2019'
loop
insert into dimCalendar select CurrentDate, EXTRACT(DOW FROM
current_date), EXTRACT(DOY FROM current_date);
CurrentDate := CurrentDate + 1;
end loop
end
Next time, try to include all the code please
Always use ISO date format, unless you have a reason not to
declare goes before begin
You're missing a semi-colon after "end loop"
Use snake_case in PostgreSQL please
create or replace function foo() returns void as $$
declare
counter integer := 1;
curr_date date := '2018-01-01';
begin
while curr_date < '2019-01-01' loop
raise notice 'curr_date: %', curr_date;
curr_date := curr_date + 1;
end loop;
end
$$ language plpgsql;
This question already has answers here:
How to declare a variable in a PostgreSQL query
(15 answers)
Closed 5 years ago.
I am building a query for a report and it has a lot of repeated const values in calculated columns. Simplified example:
select
c.title || ' corp', -- more than one column appending this
to_char(c.last_contact, 'YYYY-MM-DD HH24:MI') -- this format is used for other columns as well
from clients c;
I want to extract those repeated const values into variables and use variables instead. In TSQL I could achieve it with code like this:
declare
#suffix varchar(8) = ' corp',
#dateFormat varchar(16) = 'YYYY-MM-DD HH24:MI' -- let's pretend that's a correct format string for TSQL
select
c.title + #suffix,
convert(varchar(32), c.last_contact, #dateFormat)
from clients c
I'm trying to achieve same behavior in pl/pgsql with this:
do $$
declare
suffix text := ' corp';
date_format text := 'YYYY-MM-DD HH24:MI';
begin
select
c.title || suffix,
to_char(c.last_contact, date_format)
from clients c;
end;
$$;
And it breaks:
ERROR: query has no destination for result data
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Where: PL/pgSQL function inline_code_block line 5 at SQL statement
1 statement failed.
So far I've only found examples of using variables in WHERE block, but none in SELECT block.
I've seen EXECUTE 'select ...' WITH examples, but my quick attempt of using that
do $$
declare
process_start timestamp := now();
begin
execute 'select $1;'
using process_start;
end;
$$;
just reports DO executed successfully without actually selecting anything.
Is there a way to use pl/pgsql variables in calculated columns?
Any other alternatives to achieve this in postgres?
What do you want to do with this value? Puzzling example.
Using a FUNCTION it could be solved more or less like this
create or replace function my_function()
returns timestamp as $$
declare
process_start timestamp := now();
begin
return process_start;
end;
$$
language plpgsql
If you really want to ignore the result of the only select in your code, you can use PERFORM
do $$
declare
process_start timestamp := now();
begin
perform (select process_start);
end;
$$;
To use SELECT in PL/pgSQL, you have to specify an INTO clause:
SELECT process_start, s.title || suffix
INTO var1, var2
FROM ...