Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 784
normalUser221 Avatar asked Dec 16 '25 13:12

normalUser221


1 Answers

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);
like image 63
fancyPants Avatar answered Dec 19 '25 06:12

fancyPants



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!