Doubt in select de sql

Asked

Viewed 151 times

7

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

Example of how I wish:

select * from tabela where dt_inicial => (valores entre eles) <=dt_final mostrando o num

3 answers

8


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

The * in select means return all fields in the table, and provided that dt_initial and dt_final are of the correct date type.

If you only want the

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

Complement to the answer

Based on the comment you made to the @Edgar-Muniz-Berlinck response, if you pass dt_initial and dt_final as parameters then your table must be with the wrong modeling. Considering that the correct table would contain a column of name dt_occurrence, and receiving dt_initial and dt_final as query parameters, your sql would be:

select num from tabela where dt_ocorrencia BETWEEN dt_inicial and dt_final
  • His business rule can specify that he needs to know the time elapsed in the event, or when he started or when he finished. cannot say that the modeling is incorrect without understanding it completely. -1

  • 1

    OK, then I should say that the question is misspelled because if the business rule is this one you quoted, it should be described in the question so that we can more easily assist. Maybe I was wrong to use the term, and I should have said, you must have been wrong in your modeling.

5

select num
from tabela
where 
  'a data que voce quer' between data_inicial and data_final
  • So in this case it selects the date I want if it is between the initial and finaldata_? Because I play them as a parameter!

2

You can use too >=, <= and the operator AND, but you need to repeat the field searched.

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

Remembering it’s just more of a way of doing.

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

Browser other questions tagged

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