How to return the records of the last 7 days counting the current day with postgresql?

Asked

Viewed 1,106 times

1

I’m doing a database search where I want to return the records from the last seven days. I made an SQL script that returns the records of the last 7 days but it does not count the current day as the first day of the 7. For example if I add a new item in the table with today’s date and run the script it will only show me this new record when it is completed one day. Follows the script:

SELECT * FROM postagem_pagina WHERE postagem_pagina.data_postagem BETWEEN CURRENT DATE - 7 AND CURRENT DATE
  • 1

    I have no experience with postgresql, but this seems to be the classic case where the data field also contains the time and the query ignores this. For example, in the bank data_postagem = '03/11/2015 14:00' (two o'clock today) and, in query, CURRENT DATE = '03/11/2015' (or equivalent to '03/11/2015 00:00' that is, the beginning of today, which leaves out the registration entered later).

1 answer

5


If you do not need to give a time limit, you can find everything after the current day -7

select * from postagem_pagina where data_postagem > current_date - interval '7 days'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.