简体   繁体   English

有没有办法减去 2 个时间戳并在 Redshift 中以 'hh:mm:ss' 格式获得结果

[英]Is there a way to subtract 2 timestamps and get the result in 'hh:mm:ss' format in Redshift

I've been trying to get the duration between two timestamps using我一直在尝试使用两个时间戳之间的持续时间

select timestamp1-timestamp2 as time_s
from table_name

but I'm getting result as:但我得到的结果是:

00:00:06.070627
00:00:33.415313
00:00:51.293319
00:02:00.146453
00:02:25.600623
00:06:37.811005
00:28:27.698517

I don't want the values after ' .我不想要 ' 之后的值. ' '

I tried using我尝试使用

split_part(time_s, '.',1)

but it gave但它给了

6070627
33415313
51293319
120146453
145600623
397811005
1707698517

Can somebody please help?有人可以帮忙吗?

The difference between two timestamps is an INTERVAL .两个时间戳之间的差异是INTERVAL

The basic problem that you are experiencing is that there is no standard way to display or return an Interval.您遇到的基本问题是没有显示或返回间隔的标准方法。 For example:例如:

  • If you try your query in the web-based Redshift Query Editor, it will return NULL如果您在基于 Web 的 Redshift 查询编辑器中尝试查询,它将返回 NULL
  • If you run it in your particular SQL Client, it is returning 00:00:06.070627如果您在特定的 SQL 客户端中运行它,它将返回00:00:06.070627
  • If you run it in DbVisualizer, it is returning 0 years 0 mons 0 days 0 hours 0 mins 0.0 secs如果您在 DbVisualizer 中运行它,它将返回0 years 0 mons 0 days 0 hours 0 mins 0.0 secs
  • If you run it in DataGrip, it is returning Invalid character data was found如果您在 DataGrip 中运行它,它会返回Invalid character data was found

When you were attempting to extract a portion of the result to the left of the .当您尝试将结果的一部分提取到. , Redshift was confused because it does not see the output the way it is displayed in your SQL Client . ,Redshift 很困惑,因为它看不到 output 在您的 SQL 客户端中显示的方式

The best way to avoid your SQL Client from doing strange things to results is to convert the answer to TEXT :避免 SQL 客户端对结果做奇怪事情的最好方法是将答案转换为 TEXT

SELECT (timestamp1 - timestamp2)::TEXT

This gives an answer like: 24 days 13:57:40.561373这给出了如下答案: 24 days 13:57:40.561373

You can then manipulate it like a string:然后你可以像操作字符串一样操作它:

SELECT SPLIT_PART((timestamp1 - timestamp2)::TEXT, '.', 1)

This gives: 24 days 13:57:40这给出: 24 days 13:57:40

Bottom line: When things look strange in SQL results, cast the result to TEXT or VARCHAR to see what the data really looks like.底线:当 SQL 结果看起来很奇怪时,将结果转换为 TEXT 或 VARCHAR 以查看数据的真实情况。

暂无
暂无

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

相关问题 Redshift:将 HH:MM:SS 格式的字符串字段转换为秒和向后 - Redshift: Conversion of string field in HH:MM:SS format to seconds and backwards 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 秒到 HH:mm:SS 的时间转换 - Time Conversion of seconds to HH:mm:SS 如何将 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 如何将字符串日期 dd-mmm-yyyy hh:mm 特定格式转换为日期格式 - How to convert String date dd-mmm-yyyy hh:mm specific format to Date format 将日期 isoString "2022-12-20T21:20:23.500Z" 转换为格式 DD/MM/YYYY hh:mm in Athena sql - Convert date isoString "2022-12-20T21:20:23.500Z" to format DD/MM/YYYY hh:mm in Athena sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM