[英]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:例如:
00:00:06.070627
如果您在特定的 SQL 客户端中运行它,它将返回00:00:06.070627
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
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.