CAST function in Cassandra

CAST function helps in Converts data from one data type to another data type in Cassandra.

In Cassandra CAST function Supported in SELECT statements. lets have a look how we can used CAST function in select statement.

SELECT CAST([fieldname] AS [data type]) 
FROM [table name] 

Basic function of CAST:

  • It Converts From any native data type to text data type in formats such that ASCII and UTF-8.
  • It Converts Between numeric data types such that from `int` to `smallint`, `smallint` to `int`, etc.
  • By using the CAST function we can Converts the Most common use case.
  • It is very helpful in case of optimization where we have the need to frequent change from one data type to another.
  • By using the CAST function we can converts timestamp to text for display purposes in Cassandra.

This is a table for reference in which all native data type which can convert from one data type to another.

Lets have a look,



Table: CAST conversion table
The following table describes the conversions supported by the cast function. Cassandra will silently ignore any cast converting a datatype into its own datatype.

From To
ascii text, varchar
bigint tinyint, smallint, int, float, double, decimal, varint, text, varchar
boolean text, varchar
counter tinyint, smallint, int, bigint, float, double, decimal, varint, text, varchar
date timestamp
decimal tinyint, smallint, int, bigint, float, double, varint, text, varchar
double tinyint, smallint, int, bigint, float, decimal, varint, text, varchar
float tinyint, smallint, int, bigint, double, decimal, varint, text, varchar
inet text, varchar
int tinyint, smallint, bigint, float, double, decimal, varint, text, varchar
smallint tinyint, int, bigint, float, double, decimal, varint, text, varchar
time text, varchar
timestamp date, text, varchar
timeuuid timestamp, date, text, varchar
tinyint tinyint, smallint, int, bigint, float, double, decimal, varint, text, varchar
uuid text, varchar
varint tinyint, smallint, int, bigint, float, double, decimal, text, varchar

Source – Cassandra.Apache.org

CQL query for CAST function:
Let’s take an example: movies is a table name in which we want to change its native data type such that movie_date is field name which have timestamp data type and if we want to convert it into another native data type such that in text data type.

To create table used the following CQL query.

CREATE TABLE movies
 (
  movie_id int,
  movie_date timestamp,
  PRIMARY KEY (movie_id)
 ); 

Insert the following data into the table:

movie_id : 7c3cffb8-0dc4-1d27-af24-c007b5fc5643
movie_date : 2019-10-15 01:11:50.000000+0000  

INSERT INTO movies (movie_id, movie_date) 
       VALUES (7c3cffb8-0dc4-1d27-af24-c007b5fc5643, 
                 '2019-10-15 01:11:50.000000+0000 '); 

So, here is the format of how we can convert from one data type to another by using the CAST function. The below-given statement means that we are going to convert movie_date timestamp to movie_date text.

SELECT CAST(movie_date AS text) 
  • Result: Without CAST
    SELECT movie_date
    FROM movies
    WHERE movie_id = 7c3cffb8-0dc4-1d27-af24-c007b5fc5643; 

    Output:

    2019-10-15 01:11:50.000000+0000 (time stamp format) 
  • Result: With CAST
    SELECT CAST(movie_date AS text)
    FROM movies
    WHERE movie_id = 7c3cffb8-0dc4-1d27-af24-c007b5fc5643; 

    Output:

    2019-10-15 01:11:50.000Z (Coordinated Universal Time, or UTC) 

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.