skip to Main Content

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

2

Answers


  1. You can do it as follows :

    SELECT *, completion_date - INTERVAL years_valid_for YEAR 
    FROM mytable
    

    Demo here

    Login or Signup to reply.
  2. Syntax:- DATE_SUB(date, INTERVAL value interval)

    SELECT DATE_SUB(completion_date, INTERVAL years_valid_for YEAR) AS res_date FROM your_table_name
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search