[英]Extract (year from timestamp) in MonetDB
I try to extract year from timestamp in monetdb using 我尝试从monetdb中的时间戳中提取年份
select extract(year from ts), ts from orders;
but this query obviously returns false result: 但是这个查询显然会返回false结果:
| year | ts |
----------------------------
| 17935405 | 1441695291096 |
| 18245391 | 1441695601082 |
| 18331748 | 1441695687439 |
If the data type of the TS
column is just the epoch timestamp, the milliseconds from 1970-01-01 00:00:00.000
, it needs to be converted to a temporal type before the EXTRACT
will work. 如果TS
列的数据类型只是纪元时间戳,即1970-01-01 00:00:00.000
的毫秒数,则需要在EXTRACT
工作之前将其转换为时间类型。 According to this link : 根据这个链接 :
The convert function is called
epoch(int)
. convert函数称为epoch(int)
。 There is also a new function epoch(timestamp) which returns the number of seconds since epoch. 还有一个新的函数纪元(timestamp),它返回自纪元以来的秒数。
As I can not verify this without having a MonetDB instance, I researched a bit more, and this seems to be uglier but more likely working to get a timestamp from the epoch time (Based on this post ): 由于我无法在没有MonetDB实例的情况下验证这一点,我研究了一下,这看起来更加丑陋,但更有可能从纪元时间获得时间戳(基于这篇文章 ):
select TIMESTAMP '1970-01-01 00:00:00' + interval CAST(ts/1000 AS STRING) seconds from orders;
So if this is working, the complete solution would be something like 因此,如果这是有效的,完整的解决方案将是这样的
select EXTRACT(YEAR from (TIMESTAMP '1970-01-01 00:00:00' + interval CAST(ts/1000 AS STRING) seconds)) from orders;
Note: syntax is most likely way off as I have no MonetDB instance to test with 注意:语法最有可能因为我没有要测试的MonetDB实例
Edit: added CAST(... as STRING) as per this link I hope this does work with arithmetics like ts/1000
without issues... 编辑:添加CAST(...作为STRING)根据此链接我希望这适用于像ts/1000
这样的算术没有问题...
The following will work. 以下将有效。
select extract(year from (TIMESTAMP '1970-01-01 00:00:00' + CAST(1441695291096/1000 AS STRING)));
...returns: ...的回报:
2015
...and this: ...和这个:
select extract(year from (TIMESTAMP '1970-01-01 00:00:00' + CAST(1481767624000/1000 AS STRING)));
...returns: ...的回报:
2016
So for your query: 所以对于你的查询:
select extract(year from ts), ts from orders;
...try: ...尝试:
select extract(year from (TIMESTAMP '1970-01-01 00:00:00' + CAST(ts/1000 AS STRING))) as "Year" from orders;
...and it should work. ......它应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.