Search code examples
sqldatetimegoogle-bigquerydashboardlooker-studio

DataStudio and SQL change time format from numbers to hh:mm:ss


I want to change the metric format from float to HH:MM:SS in Datastudio (or SQL).

For Example:

Float HH:MM:SS
75 00:01:15
90 00:01:30
79.5 00:01:20

etc........


Solution

  • If your input seconds fit into 24 hours - so can be presented as a time type - use below

    select format_timestamp('%T', timestamp_seconds(cast(seconds as int64))) time   
    

    if applied to sample data in your question - output is

    enter image description here

    In case if input seconds exceed 24 hours - you can use below - which gives you a string representation in format of H*:MM:SS

    select format('%i:%s',
        div(cast(seconds as int64), 3600),
        format_timestamp('%M:%S', timestamp_seconds(mod(cast(seconds as int64), 3600)))
      ) time
    

    with output as

    enter image description here