简体   繁体   English

在 SQL 上将时间转换为 integer

[英]Converting time into an integer on SQL

I am trying to calculate the average of ride_length (hh:mm:ss format) on Big Query.我正在尝试在 Big Query 上计算 ride_length(hh:mm:ss 格式)的平均值。 ride_length For example, I am aiming for a result result that says 1 instead of 01:00:00. ride_length例如,我的目标结果是 1 而不是 01:00:00。

I tried using the CAST function to convert it into an integer but it didn't work.我尝试使用 CAST function 将其转换为 integer 但它没有用。 I also tried following the steps of answers to questions similar to mine but they didn't work as well.我也尝试按照与我类似的问题的答案步骤进行操作,但效果不佳。

You can use Extract function which will return the specified part of the time expressions.您可以使用Extract function,它将返回时间表达式的指定部分。 You can try the below queries.您可以尝试以下查询。

To extract hour from the TIME expression you can consider the below query.要从 TIME 表达式中提取小时,您可以考虑以下查询。

Query:询问:

SELECT EXTRACT(HOUR FROM TIME "01:00:00") as hour;

Output: Output:

在此处输入图像描述

To extract hour from TIMESTAMP expression below query can be considered.可以考虑从以下查询中的 TIMESTAMP 表达式中提取小时。

Query:询问:

select EXTRACT(hour from timestamp "2023-01-13 15:30:00" AT TIME ZONE "America/Los_Angeles") as hour

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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