Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ERROR: syntax error at or near "$func$ postgres error

I’m trying to create the function –

CREATE OR REPLACE FUNCTION to_tstz_immutable(t text)
  RETURNS timestamptz
  LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
$func$
BEGIN 
    RETURN date_trunc('hour', t);
END
$func$;

When I run this function I get below error –

ERROR: syntax error at or near "$func$

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

BEGIN

RETURN date_trunc(‘hour’, t);

END

$func$"
LINE 4: $func$
^

SQL state: 42601
Character: 128

What is the problem with this function?

>Solution :

Your function has several syntax issues:

  • You needs as prior to dollar quoting. So perhaps as $$function$$
  • You cannot use the data_trunc() function with a text argument for the date parameter. I.e. must be date, timestamp, timestamptz.
  • An SQL function does not use/need begin ... end

Adjusting your function for the above becomes:

create or replace function to_tstz_immutable(t timestamptz)
  returns timestamptz
  language sql immutable strict parallel safe
as $func$
   select date_trunc('hour', t);
$func$;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading