在MonetDB中提取(从时间戳开始的年份)

时间:2016-08-25 15:10:50

标签: sql monetdb

我尝试使用

从monetdb中的时间戳中提取年份
select extract(year from ts), ts from orders;

但是这个查询显然会返回错误的结果:

 |   year   |     ts        | 
 ----------------------------
 | 17935405 | 1441695291096 |
 | 18245391 | 1441695601082 |
 | 18331748 | 1441695687439 |

2 个答案:

答案 0 :(得分:0)

如果TS列的数据类型只是纪元时间戳,即1970-01-01 00:00:00.000的毫秒数,则需要在EXTRACT工作之前将其转换为时间类型。根据{{​​3}}:

  

转换函数名为epoch(int)。还有一个新功能   epoch(timestamp),返回自纪元以来的秒数。

由于我无法在没有MonetDB实例的情况下验证这一点,我研究了一下,这看起来更加丑陋,但更有可能从纪元时间获得时间戳(基于this link):< / p>

select TIMESTAMP '1970-01-01 00:00:00' + interval CAST(ts/1000 AS STRING)  seconds from orders;

因此,如果这样做,完整的解决方案将类似于

select EXTRACT(YEAR from (TIMESTAMP '1970-01-01 00:00:00' + interval CAST(ts/1000 AS STRING) seconds)) from orders;

注意:语法最有可能因为我没有要测试的MonetDB实例

编辑按照this post添加了CAST(...作为STRING)我希望这对ts/1000之类的算术没有问题...

答案 1 :(得分:0)

以下内容可行。

select extract(year from (TIMESTAMP '1970-01-01 00:00:00' + CAST(1441695291096/1000 AS STRING)));

...返回:

2015

......而且这个:

select extract(year from (TIMESTAMP '1970-01-01 00:00:00' + CAST(1481767624000/1000 AS STRING)));

...返回:

2016

所以对于你的查询:

select extract(year from ts), ts from orders;

...试试:

select extract(year from (TIMESTAMP '1970-01-01 00:00:00' + CAST(ts/1000 AS STRING))) as "Year" from orders;

......它应该有用。

相关问题