Salvador Martinez
Salvador Martinez

Reputation: 31

How can I subtract 2 string type times in MM/DD/YYYY HH:MM using SQL?

I'd like to be able to take one date/time in MM/DD/YYYY HH:MM stored as a string and subtract them to get elapsed time in minutes.

For Example

the for entry -> startdate: 1/26/2018 12:07

and the entry -> enddate: 1/29/2018 8:27

the time in between is 3 days, 3 hours, 41 minutes and 0 seconds or 4541 minutes.

Does datetime / datediff even support this function to find elapsed time? If so how can I convert this string into datetime? Or is there another way?

Upvotes: 0

Views: 320

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271231

You want str_to_date() and then some other functions.

select (to_seconds(str_to_date(enddate, '%m/%d/%Y %H:%i')) - 
        to_seconds(str_to_date(startdate, '%m/%d/%Y %H:%i'))
       ) / 60
. . .

Upvotes: 2

Related Questions