Trimming and converting a Date/Time Field

DataLawton
DataLawton Member
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:

2021-12-31T21:20:49

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


Thank you for your help

Best Answer

  • GrantSmith
    GrantSmith Coach
    Answer ✓

    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 https://domo-support.domo.com/s/article/360043429953?language=en_US

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

Answers

  • GrantSmith
    GrantSmith Coach
    Answer ✓

    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 https://domo-support.domo.com/s/article/360043429953?language=en_US

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