获取cassandra cql select中的当前日期 [英] Get current date in cassandra cql select
问题描述
在SQL中,我能够做到:
In SQL, I am able to do:
select getdate(), getdate() - 7
返回当前日期以及当前日期-7天。我想在Cassandra CQL中实现相同的目的。我试过了:
Which returns the current date as well as current date - 7 days. I want to achieve the same in Cassandra CQL. I tried:
select dateof(now())
但这不起作用。它仅适用于插入,不适用于选择。我怎么能得到相同的?
But that does not work. It works only on insert and not in select. How can I get the same? Any help would be appreciated.
推荐答案
select dateof(now())
单靠正确,这是行不通的。但是,如果您有一个只知道一行的表(例如 system.local
):
On its own, you are correct, that does not work. But if you have a table that you know only has one row (like system.local
):
aploetz@cqlsh:stackoverflow> SELECT dateof(now()) FROM system.local ;
dateof(now())
--------------------------
2015-03-26 03:18:39-0500
(1 rows)
很遗憾,Cassandra CQL不提供支持( CASSANDRA-5505 )不包含支持对于算术运算,更不用说日期算术了。因此,从该值中减去7天是您在应用程序级别中必须要做的事情。
Unfortunately, Cassandra CQL does not (yet? CASSANDRA-5505) include support for arithmetic operations, let alone date arithmetic. So subtracting 7 days from that value is something that you would have to do in your application level.
编辑20200422
较新的语法使用 toTimestamp()
函数代替:
The newer syntax uses the toTimestamp()
function instead:
aploetz@cqlsh> SELECT toTimestamp(now()) FROM system.local;
system.totimestamp(system.now())
----------------------------------
2020-04-22 13:22:04.752000+0000
(1 rows)
这两种语法在20200422之前都有效。
Both syntaxes work as of 20200422.
这篇关于获取cassandra cql select中的当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!