Shawn
Shawn

Reputation: 1

MySQL Subtract Date Time and insert results into a new Column

I have a MySQL database, I use Heidi SQL to access it. The following columns are part of one of my tables:

START_DATE START_TIME STOP_DATE STOP_TIME
01-10-2022 02:30:00 01-10-2022 03:30:00

What I need is to subtract start date/time from stop date/time and then create or insert into a 5th column called duration (in minutes), in this case the 5th column would get populated with 60.

Is this even possible?

Thanks

Upvotes: 0

Views: 410

Answers (1)

Aegis
Aegis

Reputation: 26

See TIMESTAMPDIFF

Since the DURATION column is to be the 5th column, I am assuming that each of the columns above are separated.

Using MySQL, you could try:

    SELECT *,
      TIMESTAMPDIFF(MINUTE,START_TIME, STOP_TIME) AS DURATION
    FROM
     TABLENAME;

The resulting query will display all your previous columns and add on the DURATION column as well. You may save the query to a table when you are done.

All of this is assuming that your working columns, as above, are in DATETIME. To check the information about a table's structure: DESCRIBE

Upvotes: 1

Related Questions