[英]Redshift: Conversion of string field in HH:MM:SS format to seconds and backwards
I've been searching for an easy way to convert a string field from HH:MM:SS
format to seconds in Redshift, since the TIME_TO_SEC
function doesn't exist.我一直在寻找一种简单的方法将字符串字段从
HH:MM:SS
格式转换为 Redshift 中的秒数,因为TIME_TO_SEC
function 不存在。
I have a call center database with a string field in this format, HH:MM:SS
.我有一个呼叫中心数据库,其中包含一个格式为
HH:MM:SS
的字符串字段。 For example, if it was 00:05:10
then I would need to convert it to 310
.例如,如果它是
00:05:10
那么我需要将它转换为310
。 I came up with the following on my own but there's got to be a better way:我自己想出了以下方法,但必须有更好的方法:
(SPLIT_PART("HANDLE TIME", ':', 1) * 3600) + (SPLIT_PART(SPLIT_PART("HANDLE TIME", ':', 2),':', 1) * 60) + CAST(SPLIT_PART("HANDLE TIME", ':', 3) AS INT)
Once I sum up the seconds I need to convert the seconds back to the HH:MM:SS
format.一旦我总结了秒数,我需要将秒数转换回
HH:MM:SS
格式。 So 1249
would become the string 00:20:49
.所以
1249
将成为字符串00:20:49
。 So simple in MYSQL, not so much in RedShift.在 MYSQL 中如此简单,在 RedShift 中则不然。
Any help would be greatly appreciated.任何帮助将不胜感激。
Unfortunately, I don't have Redshift on hand, but you can do something like:不幸的是,我手头没有 Redshift,但您可以执行以下操作:
select datediff(second,
timestamp('2000-01-01'),
timestamp('2000-01-01 ' || handle_time)
)
Redshift doesn't have functions specifically for this. Redshift 没有专门用于此的功能。 But, it has a great feature, called UDF , which supports python, hence it will be the perfect fit.
但是,它有一个很棒的功能,称为UDF ,它支持 python,因此它将是完美的选择。
1.To convert seconds to hh:mm:ss (or days, hh:mm:ss), create this UDF in redshift - 1.要将秒数转换为 hh:mm:ss(或天数,hh:mm:ss),请在 redshift 中创建此 UDF -
CREATE FUNCTION to_hhmmss (a bigint)
RETURNS character varying
STABLE
AS $$
import datetime
return str(datetime.timedelta(seconds=a))
$$ LANGUAGE plpythonu;
It accepts an 'big integer' (seconds) value as a parameter and returns a string (time in hh:mm:ss format)它接受一个“大整数”(秒)值作为参数并返回一个字符串(hh:mm:ss 格式的时间)
Example Output -示例输出 -
db=# select to_hhmmss_1(12004);
to_hhmmss_1
-------------
3:20:04
(1 row)
db=# select to_hhmmss_1(120040);
to_hhmmss_1
----------------
1 day, 9:20:40
(1 row)
2.To convert hh:mm:ss to seconds - 2.将 hh:mm:ss 转换为秒 -
CREATE FUNCTION to_seconds (time_str varchar(20))
RETURNS integer
STABLE
AS $$
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
$$ LANGUAGE plpythonu;
This function will accept a string (time in hh:mm:ss) and return an integer (seconds).此函数将接受一个字符串(时间单位为 hh:mm:ss)并返回一个整数(秒)。 Example Output -
示例输出 -
db=# select to_seconds('1:00:00');
to_seconds
------------
3600
(1 row)
You can use cast and trim (but you can also use extract)您可以使用 cast 和 trim(但您也可以使用 extract)
Using cast and trim:使用铸造和修剪:
select
'10:20:23' as time_col,
cast(trim(split_part(time_col, ':', 1)) as int) * 3600 + cast(trim(split_part(time_col, ':', 2)) as int) * 60 + cast(trim(split_part(time_col, ':', 3)) as int) as total_seconds
time_col | total_seconds
10:20:23 | 37223
if you have mm:ss instead you can just "shift" everything down如果你有 mm:ss 而不是你可以把所有东西都“移”下来
select
'20:23' as time_col,
cast(trim(split_part(time_col, ':', 1)) as int) * 60 + cast(trim(split_part(time_col, ':', 2)) as int) as total_seconds
time_col | total_seconds
20:23 | 1223
Using Extract:使用提取物:
select
'10:20:23' as time_col,
extract(hour from time_col::time) as hour,
extract(minute from time_col::time) as minute,
extract(second from time_col::time) as second,
hour * 3600 + minute * 60 + second as total_seconds
time_col|hour|minute|second|total_seconds
10:20:23|10 |20 |23 |37223
Extract doesn't work well with MM:SS since you have to cast as time. Extract 不适用于 MM:SS,因为您必须按时间投射。
Going back to a timestamp/time format:回到时间戳/时间格式:
to_char(date_trunc('second', interval '1 second' * total_seconds), 'HH24:MI:SS') as time_format
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.