Display the Last Insert/Update Timestamp for a Table Column Row in YCQL

A table contains a timestamp representing the date and time that a write occurred to a column.

Using the WRITETIME function in a SELECT statement returns the timestamp that the column was written to the YugabyteDB database.

The output of the function is microseconds.

Example:

				
					[root@localhost ~]# ycqlsh -u cassandra

cassandra@ycqlsh> CREATE TABLE test.stuff(c1 INT PRIMARY KEY, c2 TIMESTAMP);
  
cassandra@ycqlsh> INSERT INTO test.stuff (c1, c2) VALUES (1, currenttimestamp());

cassandra@ycqlsh> SELECT c2, writetime(c2) FROM test.stuff;

 c2                              | writetime(c2)
---------------------------------+------------------
 2022-10-03 19:45:17.175000+0000 | 1664826317176027

(1 rows)
				
			

The timestamp we get is actually the number of microseconds since midnight on January 1, 1970.

What if you wanted to see that converted to a human readable format?

We could use a Linux command like this…

				
					[root@localhost ~]# date +"%m/%d/%Y %I:%M:%S.%N" -d @`echo "1664826317176027/1000000" | bc -l`
10/03/2022 07:45:17.176027000
				
			

Or we can make use of YugabyteDB’s other API, YSQL!

				
					[root@localhost ~]# ysqlsh -c "SELECT TO_TIMESTAMP(1664826317176027 / 1000000.000)::TIMESTAMP WITH TIME ZONE;"
         to_timestamp
-------------------------------
 2022-10-03 19:45:17.176027+00
(1 row)
				
			

Have Fun!