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?
>Solution :
Your function has several syntax issues:
- You needs
asprior to dollar quoting. So perhapsas $$function$$ - You cannot use the
data_trunc()function with atextargument for the date parameter. I.e. must bedate,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$;