Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current date in cassandra cql select

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 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.

like image 206
Hitesh Avatar asked Mar 26 '15 06:03

Hitesh


People also ask

How do I get the current date in Cassandra?

In Cassandra Query Language now() function can be used for UTC (Universal Time) standard. Now() method is useful to insert value which is guaranteed to be unique. To insert the current time as a value then we can use timeuuid functions now() and dateof().

How do I check my Cassandra timestamp?

Procedure. Retrieve the date/time that the value Paolo was written to the firstname column in the table cyclist_points. Use the WRITETIME function in a SELECT statement, followed by the name of a column in parentheses: SELECT WRITETIME (firstname) FROM cycling.

How do I use a timestamp in Cassandra?

Enter a timestamp type as an integer for CQL input, or as a string literal in ISO 8601 formats. In Cassandra 3.4 and later, timestamps are displayed in cqlsh in sub-second precision by default, as shown below.


1 Answers

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.

like image 98
Aaron Avatar answered Sep 23 '22 12:09

Aaron