Skip to content

Instantly share code, notes, and snippets.

@kovid-rathee
Last active February 23, 2019 19:02
Show Gist options
  • Save kovid-rathee/f677695287723e3c355de22b8209df89 to your computer and use it in GitHub Desktop.
Save kovid-rathee/f677695287723e3c355de22b8209df89 to your computer and use it in GitHub Desktop.
Convert Timestamp to Seconds of Day (ignoring the date part)
drop function if exists transformation_utils.ts_to_seconds_of_day (p_timestamp timestamp);
create or replace function transformation_utils.ts_to_seconds_of_day (p_timestamp timestamp)
returns integer AS
$body$
declare
hs integer;
ms integer;
s integer;
begin
select (extract (hour from p_timestamp::time) * 60 * 60) into hs;
select (extract (minutes from p_timestamp::time) * 60 ) into ms;
select (extract (seconds from p_timestamp::time) ) into s;
select (hs + ms + s) into s;
return s;
end;
$body$
language 'plpgsql';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment