Open In App
Related Articles

CAST function in Cassandra

Improve Article
Improve
Save Article
Save
Like Article
Like

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) 
Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation course.

Last Updated : 30 Oct, 2019
Like Article
Save Article
Previous
Next
Similar Reads