How to use BETWEEN and LIKE together in a Query?

Asked

Viewed 412 times

-2

Well I have a table that contains some tables being them ( DATA_INICIAL , DATA_FINAL ) however I’m trying to generate a report that contains the data between the initial and final dates , and who puts this information is the user ( It is not something defined ) .

My search in the bank today only takes the data of the starting day and the final day yet I want every day within that range , how can I accomplish this ?

My Consultation :

SQL SERVER : SELECT * FROM dbo.NOME_DA_TABELA WHERE DT_INICIAL LIKE '$data_incio' AND  DT_FINAL LIKE '$data_final'
  • $data_start And $data_final are the variables that users will put in a form that I send to this page .
  • 1

    Do you want the selected records to have both DT_INICIAL and DT_FINAL dates between the initial and final dates provided or just one of them to be within the given period? I believe it is enough to use BETWEEN not understood this LIKE.

  • 1

    I believe you meant 'table that contains some columns'. DATA_INIAL and DATA_FINAL are of what type? If you ran and not error with ilike I believe they are varchar, in this case you need to convert to date and use the between commented above. place the ddl of the table in question.

2 answers

3


You can filter by logging with the initial date equal to or greater than the informed one and with the final date less or equal to that selected by the user. The consultation would be as follows:

SELECT * FROM dbo.NOME_DA_TABELA WHERE (DT_INICIAL >= '$data_incio' AND  DT_FINAL <= '$data_final')

1

You need a BETWEEN:

SELECT *
  FROM dbo.nome_da_tabela
 WHERE dt_inicial BETWEEN '$data_incio' AND '$data_final'

BETWEEN

Specifies a range to be tested.

Syntax

test_expression [ NOT ] BETWEEN begin_expression AND end_expression

Browser other questions tagged

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