Calculations with date in the query

3

I have had a hard time for some time. I have searched, but I have not found anything that would help me.

I need to do a calculation on a query, it is the following. I need to get the day the record was entered in the bank and then do an operation with the current date, I have to subtract the current date by the date that was entered in the bank, if the subtraction value is greater than 3, I will know that this record is late. So I need to do this to keep track of my table, and count how many are behind schedule. I know I should use COUNT(*) , DATADIFF , INTERVAL , and so on but I could not. It is possible ? How would it be? Thank you.

    
asked by anonymous 10.01.2017 / 19:08

3 answers

3
SELECT DATEDIFF(data_operacao, NOW()) AS Diferenca_Dias FROM Tabela

Hence in the language you are using you do the verification. If the result you gave is greater than 3 it is late.

    
10.01.2017 / 19:26
3

Here's an example:

SELECT curdate() hoje, Data_operacao
, DATEDIFF(curdate(), Data_operacao) as dias
, (
case 
when DATEDIFF(curdate(), Data_operacao) <= 3 then 0
else (DATEDIFF(curdate(), Data_operacao) - 3)
end 
) dias_atraso
FROM NOME_SUA_TABELA

Total delay is just adding up the total return of the table.

SELECT 
 sum(
case 
when DATEDIFF(curdate(), Data_operacao) <= 3 then 0
else (DATEDIFF(curdate(), Data_operacao) - 3)
end 
) as TOTAL_atraso
FROM NOME_SUA_TABELA

I hope I have helped.

    
10.01.2017 / 19:26
2

You will use DATEDIFF to assist in solving the problem.

I advise you to visit this site: link

Just follow the tutorial as you get angry.

I hope I have helped!

    
22.10.2018 / 22:54