normalUser221
normalUser221

Reputation: 41

SQL, convert integer for Timestamp subtraction

Assume we have a relation R(A, B), with A contains int values and B contains Timestamps.

We have to calculate: (time in B in minutes) - (int to minutes).

Example: (125, "2017-06-01 16:23:00")

16:23:00 = 983 min
125 = 125min

983 - 125 = 858min

The elements of A represent minutes, my problem is to convert an integer value >59 to hh:mm, since MAKETIME(hh, mm, ss) only works in the range 0 to 59.

Upvotes: 3

Views: 729

Answers (1)

fancyPants
fancyPants

Reputation: 51928

There's no need at all to convert the time of your timestamp column in minutes.

Just do

SELECT B - INTERVAL A MINUTE;

If you really just want the time to be subtracted from do

SELECT TIME(B) - INTERVAL A MINUTE;

To let the date part be untouched:

SELECT CONCAT(DATE(B), ' ', TIME(B) - INTERVAL A MINUTE);

When you absolutely need the minutes afterwards:

SELECT HOUR(TIME(B) - INTERVAL A MINUTE) * 60 + MINUTE(TIME(B) - INTERVAL A MINUTE);

Upvotes: 2

Related Questions