create or replace function trunc (x timestamp)
returns date language plpgsql as $$
begin
select date_trunc('year', to_date((to_char($1,'YYYY-MM-DD')),'YYYY-MM-DD'))
exception
when undefined_column then select date_trunc('year',current_timestamp::date)
end;
$$;
syntax error at or near "when"
create or replace function trunc (x timestamp)
returns date
language plpgsql as $function$
begin
return date_trunc('year', to_date((to_char(coalesce($1, current_timestamp), 'YYYY-MM-DD')), 'YYYY-MM-DD'));
end
$function$;