MySQL subtract bigint value as year

Advertisements

I have a MySQL database which holds one column years_valid_for as bigint and another column completion_date as datetime(6). I have to subtract bigint columns number as years.

e.g. years_valid_for is 4 and completion_date is 2023-06-07. I have to subtract 4 years from 2023-06-07 and get 2019-06-07.

Is it possible? If so, how?

I have tried DATE_SUB function but could not achieve the expected result due to syntax errors

>Solution :

You can do it as follows :

SELECT *, completion_date - INTERVAL years_valid_for YEAR 
FROM mytable

Demo here

Leave a ReplyCancel reply