简体   繁体   English

Redshift:将 HH:MM:SS 格式的字符串字段转换为秒和向后

[英]Redshift: Conversion of string field in HH:MM:SS format to seconds and backwards

  1. 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)
  2. 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.

相关问题 秒到 HH:mm:SS 的时间转换 - Time Conversion of seconds to HH:mm:SS 有没有办法减去 2 个时间戳并在 Redshift 中以 'hh:mm:ss' 格式获得结果 - Is there a way to subtract 2 timestamps and get the result in 'hh:mm:ss' format in Redshift DataStudio 和 SQL 将时间格式从数字更改为 hh:mm:ss - DataStudio and SQL change time format from numbers to hh:mm:ss 无法在 pyspark 中将纪元时间戳转换为“dd-mm-yyyy HH:mm:ss”格式 - Unable to convert epoch timestamp into "dd-mm-yyyy HH:mm:ss" format in pyspark 如何在 Azure 数据工厂中将活动 output 格式化为 YYYY-MM-DD hh:mm:ss - How to format an activity output as YYYY-MM-DD hh:mm:ss in Azure data factory 如何将字符串日期 dd-mmm-yyyy hh:mm 特定格式转换为日期格式 - How to convert String date dd-mmm-yyyy hh:mm specific format to Date format 如何将 yyyy-mm-dd hh:MM:SS.mil 形式的时间戳转换为 Athena 中的纪元时间 - How to convert timestamp in the form of yyyy-mm-dd hh:MM:SS.mil to epoch time in Athena 将 SQL 服务器时区 object `yyyy-mm-dd HH:MM;SS+HH` 转换为有效的日期时间 (UTC) - Converting a SQL Server Timezone object `yyyy-mm-dd HH:MM;SS+HH` to a valid datetime (UTC) 如何将 varchar 数据类型中的列更改为时间戳? 列中的数据格式为 'yyyy-mm-ddThh:mm:ss - How to alter column in varchar datatype to timestamp? The data in the column are in the format 'yyyy-mm-ddThh:mm:ss 使用 BigQuery 将 CURRENT_TIMESTAMP 转换为 dd/mm/yyyy hh:mm 字符串 - Cast CURRENT_TIMESTAMP to dd/mm/yyyy hh:mm string with BigQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM