Skip to content

Instantly share code, notes, and snippets.

@hostelix
Created November 7, 2016 00:04
Show Gist options
  • Select an option

  • Save hostelix/f57f7719b022ed68d9260ac7f96c8fad to your computer and use it in GitHub Desktop.

Select an option

Save hostelix/f57f7719b022ed68d9260ac7f96c8fad to your computer and use it in GitHub Desktop.
Consulta sql para obtener todas las semanas del año con la fecha de inicio y fin de cada una
select
to_char(extract(week from (f1 + (i || ' weeks')::interval)),'09')::integer as semana,
(f1 + (i || ' weeks')::interval)::date as fecha_inicio_semana,
(f2 + (i || ' weeks')::interval)::date as fecha_fin_semana
from (
select f1, f1 + 6 as f2 from (
select (current_date -(extract(week from current_date) - 1 || 'weeks')::interval)::date - (extract(dow from current_date)::integer - 1) as f1) as foo1
) as foo2, generate_series(0, 52) as i
where extract(year from (f1 + (i || ' weeks')::interval)) = extract(year from current_date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment