Rewritten title: "How to subtract bigint value as year in MySQL"
P粉561438407
P粉561438407 2023-09-13 13:15:06
0
2
569

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粉561438407
P粉561438407

reply all(2)
P粉170438285

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粉726234648

You can do it as follows:

SELECT *, completion_date - INTERVAL years_valid_for YEAR 
FROM mytable

View Demo Here

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template