PostgreSQL query with no data return

2

See the following SQL code:

SELECT * FROM venda ve WHERE ve.datavenda <= DATE '28/08/2016'

The bank has the following data:

The above SQL query is not returning any results, why? can you help me? Thank you in advance!

    
asked by anonymous 07.09.2016 / 02:07

2 answers

3

This happens because datavenda field of the vendas table is of type timestamp , your query is being interpreted as follows:

SELECT * FROM venda ve WHERE ve.datavenda <= '28/08/2016 00:00:00.000000';

What does not include records 28/08/2016 02:33:36.372 and 28/08/2016 02:55:46.873 , and explains the described behavior.

There are two possible workarounds for your problem:

SELECT * FROM venda ve WHERE ve.datavenda <= '28/08/2016 23:59:59.999999';

or

SELECT * FROM venda ve WHERE ve.datavenda::date <= '28/08/2016';

I hope I have helped!

    
07.09.2016 / 02:41
1

Change your query to;

SELECT * FROM venda ve WHERE ve.datavenda::date <= '28/08/2016'
    
07.09.2016 / 02:37