Search code examples
mysqlstringdatetimesql-timestamp

MySQL String to DATE / TIME or TIMESTAMP


In my data set, the start and end time for a task is given as a string. The string contains:

'Day, Date Month YYYY HH:MM:SS GMT'
'Wed, 18   Oct   2017 10:11:03 GMT'

The previous questions on Stack Overflow do not have data in this format and I have been struggling how to convert it into DATE/TIME or TIMESTAMP. Any advice on this would be greatly appreciated!

This post was quite relevant but still does not meet my needs, as the format of the string is different in both cases:
Converting date/time string to unix timestamp in MySQL

Overall, I want to achieve a variable 'time_on_task' which takes the difference per person between their start_time and end_time. Thus, for the following data:

Person  TaskID   Start_time                      End_time
Alpha   1       'Wed, 18 Oct 2017 10:10:03 GMT' 'Wed. 18 Oct 2017 10:10:36 GMT'
Alpha   2       'Wed, 18 Oct 2017 10:11:16 GMT' 'Wed, 18 Oct 2017 10:11:28 GMT'
Beta    1       'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:12:49 GMT'
Alpha   3       'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:13:13 GMT'
Gamma   1       'Fri, 27 Oct 2017 22:57:12 GMT' 'Sat, 28 Oct 2017 02:00:54 GMT'
Beta    2       'Wed, 18 Oct 2017 10:13:40 GMT' 'Wed, 18 Oct 2017 10:14:03 GMT' 

The required output would be something like this:

Person  TaskID Time_on_task
Alpha   1      0:00:33   #['Wed, 18 Oct 2017 10:10:36 GMT' - 'Wed, 18 Oct 2017 10:10:03 GMT']
Alpha   2      0:00:12   #['Wed, 18 Oct 2017 10:11:28 GMT' - 'Wed, 18 Oct 2017 10:11:16 GMT']
Beta    1      0:00:46   #['Wed, 18 Oct 2017 10:12:49 GMT' - 'Wed, 18 Oct 2017 10:12:03 GMT']
Alpha   3      0:01:10   #['Sat, 18 Nov 2017 10:13:13 GMT' - 'Sat, 18 Nov 2017 10:12:03 GMT']
Gamma   1      3:03:42   #['Sat, 28 Oct 2017 02:00:54 GMT' - 'Fri, 27 Oct 2017 22:57:12 GMT']
Beta    2      0:00:23   #['Wed, 18 Oct 2017 10:14:03 GMT' - 'Wed, 18 Oct 2017 10:13:40 GMT']

Solution

  • You need STR_TO_DATE() to convert the string to a date. Consider:

    select str_to_date(
          'Wed, 18   Oct   2017 10:11:03 GMT',
          '%a, %d %b %Y %T GMT'
     )
    

    Yields:

    2017-10-18 10:11:03
    

    Once you strings are converted to dates, you can use timestampdiff() to compute the difference between them, and turn the result back to a time using sec_to_time():

    select
        person,
        taskID,
        sec_to_time(
                timestampdiff(
                second, 
                str_to_date(Start_time, '%a, %d %b %Y %T GMT'),
                str_to_date(End_time, '%a, %d %b %Y %T GMT')
            )
        ) time_on_task
    from mytable
    

    Demo on DB Fiddlde:

    | person | taskID | time_on_task |
    | ------ | ------ | ------------ |
    | Alpha  | 1      | 00:00:33     |
    | Alpha  | 2      | 00:00:12     |
    | Beta   | 1      | 00:00:46     |
    | Alpha  | 3      | 00:01:10     |
    | Gamma  | 1      | 03:03:42     |
    | Beta   | 2      | 00:00:23     |