Firebird - Select with date less than 10 days

Asked

Viewed 4,197 times

1

I searched but did not understand how to use the function DATEDIFF() in the FIREBIRD what I need is for the query to return me records with dates up to 10 previous days from the date current.

Obs: Understand current date by the day the query is made.

I’ll put an example that would work on mysql where I have better dominance, but what I need is an example of how I would look in FIREBIRD.

select 
pag_id as ID,
pag_data_emissao as EMISSAO,
pag_valor AS VALOR,
pag_historico AS HISTORICO,
pag_saldo AS SALDO, 
pag_debito_credito as deb_cred
from tab_banco_lanc where pag_data_emissao >= DATE_SUB(curdate(),INTERVAL 10 DAY)

2 answers

1


The Firebird DATEDIFF function returns the difference between a date and another, according to the first parameter of the function, which can be:

  • day : difference in days;
  • month : difference in months;
  • year : difference in years.

Syntax:

DATEDIFF( <YEAR | MONTH | DAY>, DATA_1, DATA_2 )

So the answer to your question:

SELECT * FROM DOCUMENTO WHERE datediff(day, :DATA, DATAHORA_INICIO) < 10;

Compare the DATEDIFF result with the number of days you want. In this case it will return all documents in the table where the difference in days between DATAHORA_INICIO, and the parameter date, :DATE, is less than 10;

  • 1

    Thank you, I was able to test with your answer, I just had to change the parameters and name of the table, but it worked right !

  • Oh yes.. I forgot to change the example according to your table!

  • I’m having a similar problem http://answall.com/questions/120596/comort-fazer-um-sql-que-consulte-apenas-dia-de-umdata

0

I got it using the DATEADD and negating the parameter, I found simpler:

select
    CONTROLE as ID,
    DATA as EMISSAO,
    VALOR AS VALOR,
    HISTORICO AS HISTORICO,
    SALDO AS SALDO,
    DEBITOCREDITO as deb_cred
from BTR06
where DATA >= DATEADD( - 5 DAY TO CURRENT_DATE ) and CODIGO=7

Browser other questions tagged

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