I have a MySQL database that contains a bigint column named years_valid_for and a datetime(6) column named completion_date. I need to subtract the year from a bigint column.
For example, years_valid_for is 4 and completion_date is 2023-06-07. I need to subtract 4 years from 2023-06-07 to get 2019-06-07.
Is it possible? If possible, how?
I tried the DATE_SUB function but couldn't achieve the expected result due to a syntax error.
P粉1704382852023-09-14 11:23:26
Syntax:- DATE_SUB(date, INTERVAL value interval)
SELECT DATE_SUB(completion_date, INTERVAL years_valid_for YEAR) AS res_date FROM your_table_name
P粉7262346482023-09-14 00:26:16
You can do it as follows:
SELECT *, completion_date - INTERVAL years_valid_for YEAR FROM mytable