Home  >  Q&A  >  body text

Rewritten title: "How to subtract bigint value as year in MySQL"

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粉561438407P粉561438407372 days ago496

reply all(2)I'll reply

  • P粉170438285

    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

    reply
    0
  • P粉726234648

    P粉7262346482023-09-14 00:26:16

    You can do it as follows:

    SELECT *, completion_date - INTERVAL years_valid_for YEAR 
    FROM mytable

    View Demo Here

    reply
    0
  • Cancelreply