Firebird - Select with date less than 10 days

1

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

Note: Understand current date by the day the query is done.

I'll put an example that would work on mysql where I have the best domain, but what I need is an example of what it would look like 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)
    
asked by anonymous 31.08.2015 / 20:06

2 answers

1

The DATEDIFF function of Firebird returns the difference between one 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 the documents in the table where the difference in days between the DATA_TIME_DATE and the date of the: DATA parameter is less than 10;

    
31.08.2015 / 20:40
0

I've been able to use DATEADD and by negating the parameter, I find it 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
    
31.08.2015 / 20:47