Trimming and converting a Date/Time Field

edited November 2022 in Magic ETL

My goal is to filter all times between the hours of 9PM-5AM, but the data in my field looks like this:


How would I trim this field to get the time exclusively and possibly convert it to 12-hour format?

Thank you for your help


  • You can use the HOUR function to return a value between 0 and 23 to filter on (in your case 9-17).

    CASE WHEN HOUR(`Timestamp`) BETWEEN 9 AND 17 THEN 'Keep' ELSE 'Exclude' END

    If you want to convert the times in a 12 hour format you can use the DATE_FORMAT function with a specific format string

    DATE_FORMAT(`Timestamp`, '%Y-%m-%d %r')

    %Y - 4 digit year

    %m - 2 digit month

    %d - 2 digit day

    %r - Time, 12-hour (hh:mm:ss followed by AM or PM)

    For more information on the different formatting options for the format string you can reference

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**