How to search by date in a timestamp

0

I have dates stored in timestamp of the form:

1999-01-08 04:05:06
1999-01-24 13:22:29

What I want is to make a query between ranges of dates that is:

Inicio: 1999-01-09
Fin: 1999-01-12

It returns the records with a date between 1999-01-09 and 1999-01-11 that is, it does not take into account the days of the end date, ie I get:

1999-01-09
1999-01-10
1999-01-11

But I also have

1999-01-12

So what I'm looking for is to get

1999-01-09
1999-01-10
1999-01-11
1999-01-12

the query I have is

select * from ingreso 
where fecha_hora between '1999-01-09' and '1999-01-12' 

How could I get the expected results?

    
asked by Juan Pinzón 09.04.2018 в 22:33
source

2 answers

2

I think the problem is that the date with day 12 has one hour greater than 00:00:00 and when doing the query as you do not place hours it puts you by default 00:00:00, you should try placing it like this:

select * from ingreso  where fecha_hora between '1999-01-09 00:00:00'
and '1999-01-12 23:59:59'
    
answered by 09.04.2018 в 22:46
0

I almost do not use between in queries with range of dates because sometimes I have seen that it does not bring all the records that are or exactly.

The way I do it is using the signs of greater equal and less equal >= and <= for your case can be this example:

select * from ingreso 
where fecha_hora >='19990109' 
and fecha_hora <= '19990112' 
    
answered by 09.04.2018 в 23:26