JOSE HERRADA
JOSE HERRADA

Reputation: 149

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

Upvotes: 0

Views: 70

Answers (1)

GMB
GMB

Reputation: 222682

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'

Upvotes: 3

Related Questions