Doubt in sql select

7

So ... I have a table that has a sequenc , a num , a dt_inicial , and a dt_final .   My question is: how to make a Select that takes the data between dt_inicial and dt_final showing num between them?

Example of how I wish:

select * from tabela where dt_inicial => (valores entre eles) <=dt_final mostrando o num
    
asked by anonymous 22.08.2014 / 13:55

3 answers

8
select * from tabela where 'data de pesquisa' BETWEEN dt_inicial AND dt_final

The * no select means return all table fields, and as long as dt_final and dt_final are of the correct date type.

If you only want num

select num from tabela where 'data de pesquisa' BETWEEN dt_inicial AND dt_final

Response Complement

Based on the comment you made to reply from @ edgar-muniz-berlinck, if you pass dt_initial and dt_final as parameters then your table should be with the wrong modeling. Considering that the correct would be if your table contains a column named dt_ocorrencia , and getting dt_final and dt_final as search parameters, your sql would be: / p>

select num from tabela where dt_ocorrencia BETWEEN dt_inicial and dt_final
    
22.08.2014 / 14:08
5
select num
from tabela
where 
  'a data que voce quer' between data_inicial and data_final
    
22.08.2014 / 14:04
2

You can also use >= , <= , and AND , but you must repeat the search field.

SELECT num FROM tabela 
WHERE 'data_pesquisa' => dt_inicial AND 'data_pesquisa' <= dt_final

Remembering that it's just another way to do it.

Using BETWEEN , as shown in the other answers, in my opinion it is much more elegant and generates the same result.

    
22.08.2014 / 14:24