简体   繁体   中英

Trimming time in SQL BigQuery

I'm currently learning SQL on BigQuery through Google's Data Analytics Certification on Coursera and I just have some confusion regarding the TRIM() function. So for starters, here's a screenshot of the data I want to use TRIM() on. enter image description here

SELECT ride_id, rideable_type, started_at, ended_at,
(SELECT (started_at - ended_at)) AS duration, 
EXTRACT(time from started_at) AS start_time,
EXTRACT(time from ended_at) AS end_time,
EXTRACT(DAYOFWEEK from started_at) AS day_of_the_week
FROM `first-project-325905.bike_2020_april.july_bike`
WHERE member_casual = 'member'

^ this is the code I ran to get the first image I linked, but I want to use TRIM to turn the result 0-0 0 -0:3:56 into 0:3:56 . I've tried using RIGHT() but it can't be applied to all the fields since some trips last days (so I can't just use RIGHT(duration, 7) since it would crop the digits from longer trips.)

I tried running this code where I casted the duration as a string and then used TRIM:

SELECT ride_id, rideable_type, started_at, ended_at,
(SELECT TRIM(CAST((started_at - ended_at)AS STRING), "0-0 0 -0:")) AS duration, 
EXTRACT(time from started_at) AS start_time,
EXTRACT(time from ended_at) AS end_time,
EXTRACT(DAYOFWEEK from started_at) AS day_of_the_week
FROM `first-project-325905.bike_2020_april.july_bike`
 WHERE member_casual = 'member'

and the results of the duration came out looking like: 3:56 enter image description here

So my question is, is there a way to use TRIM() so that the duration will look like 0:3:56 instead of 3:56? I've been trying other combinations for "0-0 0 -0:" in the second line of the second code chunk but I can't seem to get it right. For reference, I'm trying to find the average trip duration and maximum/minimum as well so I know I have to cast the data again as time after I get the TRIM() right.

Any help would be appreciated!!

Please use format:

SELECT ride_id, rideable_type, started_at, ended_at,
(
SELECT format((started_at - ended_at),'HH:mm:ss') AS duration, 
EXTRACT(time from started_at) AS start_time,
EXTRACT(time from ended_at) AS end_time,
EXTRACT(DAYOFWEEK from started_at) AS day_of_the_week
FROM `first-project-325905.bike_2020_april.july_bike`
 WHERE member_casual = 'member'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM