Skip to content

Instantly share code, notes, and snippets.

@ianthrive
Last active August 29, 2015 14:06
Show Gist options
  • Save ianthrive/41e68400e140f60a2d33 to your computer and use it in GitHub Desktop.
Save ianthrive/41e68400e140f60a2d33 to your computer and use it in GitHub Desktop.
Calculates and returns the Nth day of the week for the given date.
CREATE OR REPLACE FUNCTION public.extract_nth_dow(d date) RETURNS integer
AS $$
-- Calculates and returns the Nth day of the week for the given date.
SELECT (EXTRACT(day FROM d)::INTEGER - 1) / 7 + 1;
$$ LANGUAGE sql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment