简体   繁体   中英

Extract (year from timestamp) in MonetDB

I try to extract year from timestamp in monetdb using

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

but this query obviously returns false result:

 |   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. According to this link :

The convert function is called epoch(int) . There is also a new function epoch(timestamp) which returns the number of seconds since epoch.

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 ):

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

Edit: added CAST(... as STRING) as per this link I hope this does work with arithmetics like ts/1000 without issues...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM