Skip to content
Advertisement

Get the logs of the current week in postgresql

I need to get the logs of the current week in postgresql, I have tried to use the native mysql functions yearweek for that, but here in postgresql it is different

This is my table

CREATE TABLE dolar_euro(
id serial not null primary key,
monitordolar text,
bancocentral text,
eurocentral text,
fecha_registro date
);

Basically I need to get the records of the current week based on the records in the column fecha_registro

Advertisement

Answer

Use date_trunc():

select *
from dolar_euro
where fecha_registro >= date_trunc('week', current_date)

Note: Postgres understand weeks as defined by ISO, meaning that a week starts on Monday.

If your table may contain dates in the future, you might want an upper bound too:

select *
from dolar_euro
where fecha_registro >= date_trunc('week', current_date)
  and fecha_registro <  date_trunc('week', current_date) + interval '1 week'
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement