0

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$

BEGIN

RETURN date_trunc('hour', t);

END

$func$" LINE 4: $func$ ^

SQL state: 42601 Character: 128

What is the problem with this function?

1 Answer 1

1

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$;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.