Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I execute a comand if condition is true (SQL)

I want to compare 2 dates and if there´s a difference bigger than 2 days I want to update the field "data_fecho" from the table.

Here’s an example from the table

id message lastupdate data_fecho
1 Hello 2021-12-20 15:20:51 NULL
SELECT id,message,DATEDIFF(NOW(), lastupdate) AS dif FROM tickets;

Above I can get the difference between the dates but I can´t update it, doesn´t recognize dif

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

IF (dif > 2)
BEGIN
    UPDATE tickets set data_fecho=NOW()
END

>Solution :

You could add a where clause to your update statement:

UPDATE tickets
SET    data_fecho = NOW()
WHERE  DATEDIFF(NOW(), lastupdate) > 2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading