Get current date in cassandra cql select

后端 未结 2 1959
闹比i
闹比i 2021-01-01 16:54

In SQL, I am able to do:

select getdate(), getdate() - 7

Which returns the current date as well as current date - 7 days. I want to achieve

2条回答
  •  失恋的感觉
    2021-01-01 17:12

    select dateof(now())
    

    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)
    

    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.

    Edit 20200422

    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)
    

    Both syntaxes work as of 20200422.

提交回复
热议问题